题目描述:输入一段字符(由空格、字母和数字几种组成,保证开头不为空格),里面有若干个字符串,求这些字符串的长度和,并输出最长字符串内容,如果有多个输出最先出现的那个字符串。以stop
作为最后输入的字符串
#include<stdio.h>#include<string.h>int main (){int i,j,count,cd,max,length;char a[100];while(1){gets(a);length=strlen(a);cd=0;//用来测量总的字母数 count=0;//用来表示每个字符串长度 max=0;//用来记录最长字符串长度 if(strcmp(a,"stop")==0) break;else{for(i=0;i<=length;i++){if(a[i]!=' '&&a[i]!='\0'){count++;cd++;}else if(a[i]==' '||a[i]=='\0'){//字符串是以\0结束的所以咋也要判断到那里 if(count>max){max=count;//记录最长单词的长度 j=i;//记录最长单词后面空格的位置 }count=0;//每次的单词长度比较后都要重新归0 }}printf("%d ",cd);//输出字符串长度for(i=j-max;i<j;i++) printf("%c",a[i]);//根据j所记录的位置来输出最长字符串putchar('\n');}}return 0;}