#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
int count,i,j;
char str[50],sub_str[50];
printf("Enter the main string : ");
gets(str);
printf("Enter the sub string : ");
gets(sub_str);
printf("%s %s\n",str,sub_str);
int l_str, l_sub_str;
l_str=strlen(str);
l_sub_str=strlen(sub_str);
for(i =0 ;i<(l_str); i++)
{
if(str[i]==sub_str[0])
{
count=1;
for( j = 0; j<l_sub_str;j++)
if(str[i+j]==sub_str[j])
{
count++;
printf("%d \n",i);
}
else
break;
}
if(count==l_sub_str)
break;
}
if(count==l_sub_str)
printf("String found at position %d \n",i+1);
else
printf("No sub string \n");
for(i=0;i<l_str;i++)
printf("%c",str[i]);
printf("\n");
}
示例数据:
MAIN STRING: qwerty is
SUB STRING: is
POSITION:1
此代码用于检查字符串中是否存在子字符串,并告知其位置是否存在。在上面的代码中,它最后有输出。对于那个输出它应该显示,POSITION为8,但它显示1.为什么?
答案 0 :(得分:0)
问题与您使用gets()
没有直接关系,但是(如评论中所述)您应该never use gets()
because it is too dangerous。
麻烦在于:
j = 0
启动内部循环,这意味着您检查第一个字母两次,并将其计数两次,因此当搜索完成匹配时,您将count
设置得太大。一个简单的解决方法是使用j = 1
启动循环;另一种修复方法是在循环之前设置count = 0;
。用gets()
加fgets()
替换strcspn()
并检查EOF会产生如下代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
char str[50], sub_str[50];
printf("Enter the main string : ");
if (fgets(str, sizeof(str), stdin) == 0)
{
fprintf(stderr, "EOF\n");
exit(EXIT_FAILURE);
}
str[strcspn(str, "\n")] = '\0';
printf("Enter the sub string : ");
if (fgets(sub_str, sizeof(sub_str), stdin) == 0)
{
fprintf(stderr, "EOF\n");
exit(EXIT_FAILURE);
}
sub_str[strcspn(sub_str, "\n")] = '\0';
printf("Haystack = [%s]\nNeedle = [%s]\n", str, sub_str);
int l_str = strlen(str);
int l_sub_str = strlen(sub_str);
int count = 0;
int i;
for (i = 0; i < l_str; i++)
{
if (str[i] == sub_str[0])
{
count = 1;
for (int j = 1; j < l_sub_str; j++)
{
if (str[i + j] == sub_str[j])
count++;
else
break;
}
}
if (count == l_sub_str)
break;
}
if (count == l_sub_str)
printf("String found at position %d (%s)\n", i + 1, &str[i]);
else
printf("No sub string\n");
return 0;
}
示例输出(程序名称ss11
):
$ ./ss11
Enter the main string : qwerty is
Enter the sub string : is
Haystack = [qwerty is]
Needle = [is]
String found at position 8 (is)
$ ./ss11
Enter the main string : qwerty is
Enter the sub string : z
Haystack = [qwerty is]
Needle = [z]
No sub string
$ ./ss11
Enter the main string : qwerty is
Enter the sub string : y
Haystack = [qwerty is]
Needle = [y]
String found at position 6
$ ./ss11
Enter the main string : qwerty isosceles isomer isotropic isotopes
Enter the sub string : iso
Haystack = [qwerty isosceles isomer isotropic isotopes]
Needle = [iso]
String found at position 8 (isosceles isomer isotropic isotopes)
$ ./ss11
Enter the main string : qwerty isosceles isomer isotropic isotopes
Enter the sub string : isot
Haystack = [qwerty isosceles isomer isotropic isotopes]
Needle = [isot]
String found at position 25 (isotropic isotopes)
$ ./ss11
Enter the main string : qwerty isosceles isomer isotropic isotopes
Enter the sub string : isoto
Haystack = [qwerty isosceles isomer isotropic isotopes]
Needle = [isoto]
String found at position 35 (isotopes)
$