我在 C 中编写了一个函数,用于搜索子字符串是否在字符串中并且没问题,但是当我在字符串数组中使用它时,我遇到了错误。请看这段代码:
#include<stdio.h>
#include<conio.h>
#include<string.h>
int substring(char*, char*);
main()
{
char student[100][7];
int counter = 0;
int finish =1;
char s1[4];
while(finish){
printf("please Enter Student Number: \n");
scanf("%s", student[counter]);
counter++;
printf("Do you want to exit? 1/0");
scanf("%d", &finish);
}
printf("Now, You can search in Student numbers\n");
printf("Enter a number to search: ");
scanf("%s", s1);
for(int i = 0; i < counter; i++){
printf("%d : %s\n", i, student[i]);
if(substring(student[i],s1) == 1)
printf("%s", student[i]);
}
getch();
}
int substring(char *s1,char *s2)
{
int f=0;
for(; *s1 !='\0';)
{
if(*s2=='\0')
break;
for(;*s2 !='\0';)
{
if(*s1==*s2)
{
f=1;
s1 ++;
s2 ++;
}
else
{
f=0;
s1++;
break;
}
}
}
if(f==0)
return 0;
else
return 1;
getch();
}