輸入一段字符(只含有空格和字母,保證開頭不為空格),里面有若干個字符串,求這些字符串的長度和,並輸出最長字符串內容,如果有多個輸出最先出現的那個字符串。以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;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM