我正在使用C++
. 该程序对于大多数输入都运行良好,但是当我使两者相strings
同时,输出什么也不显示并正常返回。这可能是一个错误,但我无法找到它。我尝试遵循算法的控制流程,但仍然没有成功。我在下面给出了实现。
#include<iostream>
#include<cstring>
using namespace std;
int simple_text_search(const char* p, const char* q);
int main(){
if( int i = simple_text_search("ell", "ell")) //strings are not from standard input
cout << "Found at " << i;
return 0;
}
int simple_text_search( const char* p, const char* q){
int m = strlen(p);
int n = strlen(q);
int i = 0;
while(i + m <= n) {
int j = 0;
while(q[i + j] == p[j]){
j = j + 1;
if(j == m)
return i;
}
i = i + 1;
}
return -1;
}