我需要搜索字符串形式的隨機字符串列表以查找某個字符串。該函數將返回指向搜索列表中最後一個字符後面的字符的指針。在我的主函數中,我需要循環函數並返回隨機列表中搜索字符串的出現次數。在字符串中搜索字符串C
這是我的功能。
char *findStringInData(char *data, char *string){
int x, i;
int m = 0, n = 0;
char *follower;
char *temp;
for (x = 0; data[x] != '\0'; x++){
if (data[x]== string[x]){
temp = &data[x];
//m++;
}
for(i = data[x]; string[i] != '\0'; i++){
if (data[i] == string[i])
follower = &temp[i];
//n++;
//printf("the count is %d\n", n);
}
}
return follower;
return NULL;
}
爲什麼不直接使用['的strstr(3)' ](http://www.manpagez.com/man/3/strstr/)? – 2013-02-23 19:59:19
也許OP在一個沒有標準庫的嵌入式平臺上。 – fuz 2013-02-23 22:25:45
@FUZxxl,'strstr'的newlib實現是大約12行有意義的代碼(即不是大括號或空白行)。如果OP沒有標準庫,他肯定可以毫無問題地使用這一功能。然而,看起來他*有一個標準庫,因爲他的程序調用了'printf',儘管它確實被註釋掉了。 – 2013-02-24 18:16:44