- //编写函数int strstr(char srcstr[],char deststr[] )
- //在字符串srcstr中查找字符串deststr,如果找到,则返回字符串deststr在字符串srcstr中的位置。
- #include <stdio.h>
- #include <conio.h>
- #define MAX 256
- int strstr(char srcstr[],char deststr[]);
- int main()
- {
- char srcstr[MAX];
- char deststr[MAX];
- char c;
- int n;
- int index;
- n=0;
- printf("请输一个源字符串:/n");
- while((c=getchar()) !='/n')
- {
- srcstr[n] =c;
- n++;
- }
- n=0;
- printf("请输入一个你要查找的字符:/n");
- while((c=getchar()) !='/n')
- {
- deststr[n] =c;
- n++;
- }
- index = strstr(srcstr,deststr);
- if (index>=0)
- {
- printf("找到了,在源字符串中的位置是: %d/n", index);
- }
- else
- printf("没有找到哦/n");
- getch();
- return 0;
- }
- int strstr(char srcstr[],char deststr[] )
- {
- int j=0;
- for (int i=0;srcstr[i]!='/0';i++)
- {
- if (deststr[0]==srcstr[i])
- {
- while (deststr[j]!='/0'&&srcstr[i+j]!='/0')
- {
- j++;
- if (deststr[j]!=srcstr[i+j])
- {
- break;
- }
- }
- }
- if (deststr[j]=='/0')
- {
- return i;
- }
- }
- return -1;
- }
<script type="text/javascript"> </script> <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"> </script>