输入一段字符(只含有空格和字母,保证开头不为空格),里面有若干个字符串,求这些字符串的长度和,并输出最长字符串内容,如果有多个输出最先出现的那个字符串。以stop作为最后输入的字符串。

#include<stdio.h>
#include<string.h>
main()
{
    int i,j=0,m,count,max;
    char a[100];
    while(1)
    {
        gets(a);
        count=0;
        max=0;
        if(strcmp(a,"stop")==0)
            break;
        for(i=strlen(a)-1;i>=0;i--)
        {
            if(a[i]!=' '&&i!=0)
            {
                j++;
                count++;
            }
            else if(a[i]==' '||i==0)
            {
                if(i==0)
                {
                    j++;
                    count++;
                }
                if(j>=max)
                {
                    max=j;
                    m=i;
                    if(i==0)
                        m=i-1;
                }
                j=0;
            }
        }
        printf("%d ",count);
        for(i=m+1;i<m+1+max;i++)
            printf("%c",a[i]);
        printf("\n");
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/zhouaolong/p/12587693.html