寫一個函式查找子串在字串中出現的所有位置并回傳主函式輸出
比如主串abcaabc
子串abc 那么回傳1 5
哪位大佬會,C++小白
uj5u.com熱心網友回復:
用strstr函式有個 C 的 例子:
#include <stdio.h>
#include <string.h>
void foundPos(char* dest, const char* subs, char* buf)
{
char *pch;
int pos;
char tmpStr[10];
pch = strstr(dest,subs);
while(pch != NULL ){
pos = pch-dest;
sprintf(tmpStr,"%d",pos+1);
strcat(buf, tmpStr);
strcat(buf, " ");
pch = strstr(pch+1,subs);
}
}
int main()
{
char str[] = "abcbbbabc";
char pch[100] = {0};
printf("looking for 'abc' in \"%s\"\n ",str);
foundPos(str, "abc", pch);
printf("%s\r\n", pch);
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/191232.html
標籤:C++ 語言
上一篇:求助
下一篇:不懂,有沒有人看看怎么改
