|
发表于 2021-1-5 13:54:26
|
显示全部楼层
本帖最后由 jackz007 于 2021-1-5 13:55 编辑
- #include <stdio.h>
- // 返回值为最长字符串在 str 中的位置索引
- int maxlenwordpos(char * str)
- {
- int c , d , i , n ;
- for(c = d = i = n = 0 ; str[i] ; i ++) {
- if(str[i] == ' ') {
- if(c > n) {
- n = c ;
- d = i - c ;
- }
- c = 0 ;
- } else {
- c ++ ;
- }
- }
- if(c > n) d = i - c ;
- return d ;
- }
- int main(void)
- {
- char s[256] ;
- int i , n ;
- for(n = 0 ; n < 255 && (s[n] = getchar()) != '\n'; n ++) ;
- s[n] = '\0' ;
- i = maxlenwordpos(s) ;
- while(s[i] && s[i] != ' ') putchar(s[i ++]) ;
- printf("\n") ;
- }
复制代码
编译、运行实况
- D:\0002.Exercise\C>g++ -o x x.c
- D:\0002.Exercise\C>x
- ab cde fghi jklmno pq rst 123 2345678 uvwx yz
- 2345678
- D:\0002.Exercise\C>
复制代码 |
|