一、代码
/**
字符串去空格
*/
void trimStr(char* str){
int index=0;
int readIndex = 0;
while (*(str+readIndex)) {
if(str[readIndex]!=' '){
str[index++] = str[readIndex++];
}else{
readIndex++;
}
}
str[index] = '\0';
printf("%s\n",str);
}
/**
查找特定字符串在给定的字符串中的位置
*/
int searchStr(char* str,char* target){
//记录下一次开始比较的位置
int index = 0;
//开始比较的位置
int readIndex = 0;
//特定字符串的比较位置
int tRead = 0;
while (str[index]) {
while (str[readIndex] == target[tRead] && target[tRead]){
readIndex++;
tRead++;
}
if(!target[tRead]){
return index;
}
readIndex = ++index;
tRead = 0;
}
return 0;
}
int main(int argc, const char * argv[]) {
char src[] = " h l l o w o r l d";
trimStr(src);
int index =searchStr("helloworld", "orld");
printf("%d\n",index);
return 0;
}
2、结果