我的代码:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define SIZE 40
char *string(char *,char *);
int main(void)
{
char temp[SIZE],find[SIZE];
char *found;
puts("enter a line (empty line to quit): ");
while(gets(temp)!=NULL&&temp[0]!='\0')
{
puts("enter to be found string: ");
gets(find);
if(strlen(temp)<strlen(find))
{
puts("第二次输入超出范围,清重新输入: ");
continue;
}
found=string(temp,find);
if(found==NULL)
puts("未找到要查找的字符串!");
else
printf("%c: %p,%5s\n\n",*found,found,found);
puts("enter next: ");
}
getch();
return 0;
}
char *string(char *s1,char *s2)
{
int n=0;
char *flag;
char *temp=s1,*find=s2;
while(*temp)
{
if(*temp==*find)
{
flag=temp;
while(*find)
{
if(*temp==*find)
n++;
else
{
find=s2;
n=0;
break;
}
find++;
temp++;
}
}
if(strlen(s2)==n)
return flag;
temp++;
}
return NULL;
}
运行结果:
enter a line (empty line to quit):
123456
enter to be found string:
234
2: 0022FF41,23456
enter next:
asdasdasdfgfdgf
enter to be found string:
dasd
d: 0022FF42,dasdasdfgfdgf
enter next:
12121212222111122121
enter to be found string:
2211
未找到要查找的字符串!
enter next:
fdfdfdfddddffffdddfffdd
enter to be found string:
ddddfff
d: 0022FF47,ddddffffdddfffdd
enter next:
ddddffffddfdfdfffddff
enter to be found string:
dfff
未找到要查找的字符串!
enter next:
程序本身就有很大的问题,麻烦能在我的程序上帮我改改吗,谢谢!
我感觉我写的这个太烂了,大虾能精简我的这个程序吗,(当然能在我先阶段能理解的范围内),谢啦!