苏州中梁天御好不好:一道C语言题
#include <stdio.h>
#include <string.h>
main()
{
char buf[1001];
char maxstr[1001];
int maxlen = 0;
do
{
fgets(buf, 1000, stdin);
printf("The len is %d\n",strlen(buf));
if (strlen(buf)>maxlen)
{
strcpy(maxstr, buf);
maxlen = strlen(maxstr);
}
}while(!strcmp(buf,"stop"));
printf("The largest string is %s\n", maxstr);
}
#include <stdio.h>
char * copy(char *q)
{
int i;
char *p;
p=(char *)malloc(128*sizeof(char));
if(p==NULL)
{
printf("out of memory!\n");
exit(0);
}
for(i=0;;)
{
scanf("%s",p);
printf("%d\n",strlen(p));
if(!strcmp(p,"stop\0"))
{
if(!strcmp(q,"\0")) strcpy(q,p);
break;
}
if(strlen(p)>i)
{
i=strlen(p);
strcpy(q,p);
}
}
free(p);
return q;
}
int main(void)
{
char *p;
p=(char *)malloc(128*sizeof(char));
if(p==NULL)
{
printf("out of memory!\n");
exit(0);
}
p=copy(p);
printf("%s\n",p);
free(p);
return 0;
}