类似甩葱歌的歌曲:一道C语言题

来源:百度文库 编辑:中科新闻网 时间:2024/04/27 18:35:05
编写程序,输入若干个字符串,求出每个字符串的长度,并打印最长一个字符串的内容。以"stop"作为输入的最后一个字符串。

#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;
}