字符串倒序,以及查找 !

char* Daoxu(char *szChar)
{
    assert(szChar != NULL);  
    char *temp = szChar;
    int len = strlen(szChar);
    
    if (len == 0 || len == 1)   
    {
        return temp;       
    }
    else
    {
        for (int i=0; i<len/2; i++)
        {
            std::swap(szChar[i], szChar[len-1-i]);
        }
        return temp; 
    } 
}


int FindString(char *pCompared, const char *pStand)
{    
    assert( (pCompared != NULL) && (pStand != NULL) );
    assert( !( strlen(pCompared) < strlen(pStand) ) );
    int lenofcmp = strlen(pCompared);
    int lenofstd = strlen(pStand);
    char *temp = (char*)malloc(lenofcmp+1); 
    memset(temp, 0, lenofcmp);
    int total = 0;
       
    for (int i=0; i<lenofcmp; i++)
    {
        if ( (lenofcmp-i) < lenofstd ) 
        {
            return total;
        }
        
        memcpy(temp, pCompared+i, lenofstd);
        
        if (strcmp(temp, pStand) == 0) 
        {           
            ++total;
        }
    }
    
    return total;
}  



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我会回答你的问题。 对于第一个问题,使用String类或StringBuffer类都可以统计一个子串在整串中出现的次数。不过,使用StringBuffer类的效率更高,因为它可以直接对字符串进行修改。以下是使用StringBuffer类实现的示例代码: ```java StringBuffer s = new StringBuffer("hello world, hello java"); String str = "hello"; int count = 0; int index = -1; while ((index = s.indexOf(str, index + 1)) != -1) { count++; } System.out.println(count); ``` 对于第二个问题,以下是按照要求完成的代码: ```java StringBuffer sb1 = new StringBuffer("This is a StringBuffer"); // 查找匹配字符串ok System.out.println(sb1.indexOf("ok")); // 查找匹配字符串is System.out.println(sb1.indexOf("is")); // 查找is最后出现的位置 System.out.println(sb1.lastIndexOf("is")); // 获取字符串中第10个位置的字符 System.out.println(sb1.charAt(9)); // 添加浮点数1.23到字符串的尾部 sb1.append(1.23); System.out.println(sb1); // 删除字符串中”This “ sb1.delete(0, 5); System.out.println(sb1); // 在”is”后插入 ”hello” int index = sb1.indexOf("is"); sb1.insert(index + 2, "hello"); System.out.println(sb1); // 将字符串中”ring”替换成”apple” String str = sb1.toString().replace("ring", "apple"); sb1.replace(0, sb1.length(), str); System.out.println(sb1); // 将字符串倒序 sb1.reverse(); System.out.println(sb1); ``` 对于第三个问题,以下是一个简单的字符串排序程序的示例代码: ```java String[] strs = {"apple", "banana", "cat", "dog", "elephant"}; Arrays.sort(strs, (s1, s2) -> s1.compareTo(s2)); for (String str : strs) { System.out.println(str); } ``` 以上就是我对你提出的问题的回答,希望能够帮到你。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值