如何查找字符串是否存在于另一个字符串中,不是使用字符串比较函数,而是迭代每个字符并在C ++中测试相等性?
string one="hello world"; // Search *in* this string
string two="wor"; // Search *for* this string
答案 0 :(得分:0)
看起来像是作业^^
int find(string one, string two){
int a, b;
for(int c = 0; c + two.length() < one.length(); c++){
a = 0;
b = c;
while(a < two.length() && one[b++] == two[a++]);
if(a == two.length())return c;
}
return -1;
}