> 文档中心 > 查找字符子串

查找字符子串

程序运行结果示例1:

Input a string:How are you!↙

Input another string:are↙

Searching results:5

程序运行结果示例2:

Input a string:hello↙

Input another string:are↙

Not found!

程序运行结果示例3:

Input a string:You are a student.↙

Input another string:you↙

Not found!

#include #include int main(){char s1[80],s2[10];   printf("Input a string:");   gets(s1);   printf("Input another string:");   gets(s2);   int m=strlen(s2);   int a[10],i,j,k,o;for(i=0;s1[i]!=0;i++){if(s1[i]==s2[0]){k=i;o=1;for(j=1;s2[j]!=0;j++){if(s1[i+j]!=s2[j]){o=0;break;}}if(o==1){printf("Searching results:%d\n",k+1);break;}}}if(o==0){printf("Not found!\n");}   return 0;}