我使用这段代码来确定字符串中是否存在子字符串.但是,即使字符串中不存在子字符串,结果也是Present
.基本上,我得到了Present
分作为唯一的结果.
#include <stdio.h>
#include <string.h>
int main()
{
int i, la, lb, j, f = 0, found = 0;
char a[100], b[100];
printf("Enter a string: ");
fgets(a, 100, stdin);
printf("Enter substring: ");
fgets(b, 100, stdin);
la = strlen(a);
lb = strlen(b);
for (i = 0;i < la - 2; i++) {
if (a[i] == b[0]) {
for (j = 0; j < lb - 2; j++) {
f = 0;
if (a[i + j] != b[j]) {
f = 1;
break;
}
}
}
if (f == 0) {
found = 1;
break;
}
}
if (found == 1) {
printf("Present.");
} else {
printf("Not Present.");
}
return 0;
}