- #include <stdio.h>
- int main()
- {
- char str[]="welcome you to beijing";
- int i =0,max =0,start =0,end =0;
- while(str[i] !='\0')
- {
- int count=0;
- while(str[i] !=' '&&str[i]!='\0')
- {
- count++;
- i++;
- }
- if(count >max)
- {
- max=count;
- end =i-1;
- start =end-count;
- }
- i++;
-
- }
- for(i=start;i<=end;i++)
- {
- printf("%c",str[i]);
- }
- printf("\n");
- return 0;
- }
复制代码
但是我这代码有个缺点,如果最长的有两个单词,后面的就没法打印出来,看看大家是怎么解决这个问题的。 |
|