C语言程序设计,输入若干行字符,输出其中最长的一行。

2025-06-28 15:21:18
推荐回答(3个)
回答1:

正好前段时间编过,VC可编译运行。
代码如下,其中由n来控制读入的字符串行数。
#include
char *fun(char (*a)[11],int num)
{ int i;
char *max;
max=*a;
for(i=0;i if(strlen(max) max=*(a+i);
return max;
}
void main()
{ char ss[3][11],*p,(*q)[11];
int n,i;
printf("input the number:\n");
scanf("%d",&n);
for(i=0;i scanf("%s",ss[i]);
/*for(i=0;i printf("%s",*(*(ss+i)));*/
printf("\n");
q=ss;
p=fun(q,n);
printf("%s",p);
}

回答2:

#include #include #include void main() { int flag=1,i=0,n=0; int countw=-1; //记录最长单词的长度 char *b; //记录最长单词最后一个字符 char str[100],*temp; scanf("%[^#]",str); //输入字符串,以#结束 temp=str; do { if(isalpha(*temp)) n++; /* 如果当前字符为字母,则临时单词长度加1;否则判断是否为最长单词 */ else{ if(n>0 && n>countw) { b=temp-1; countw=n; } n=0; } temp++; }while(*(temp-1)); printf("\n最长的单词:\n",str); for(i=b-countw+1;i<=b;i++) printf("%c",*i); printf("\n"); } ~

回答3:

//哪里需要写得像一楼那么复杂 输入任意行 直接回车 结束输入

12314516
234234
wrwetr
fsdf

the max long is 12314516
Press any key to continue

#include
#include
main()
{
int maxl=0;
char str[80],strMaxl[100];
while (str[0]!='\0')
{
gets(str);
if (strlen(str)>maxl)
{
maxl=strlen(str);
strcpy(strMaxl,str);
}
}
printf("the max long is %s\n",strMaxl);
}