void FindLongWordchar h char word int i=0j=0; int len=0; whilehi !=0 j = i; whilehj !=0 && hj != j++; len = j-i; iflen strle
可以简化,可以使用指针来遍历字符串,同时可以去掉变量len,直接在判断长度时计算。
简化后的代码如下:
void FindLongWord(char h[], char word[]) { int max_len = 0; char *p = h; while (*p != '\0') { int len = 0; while (*p != '\0' && *p != ' ') { len++; p++; } if (len > max_len) { max_len = len; strncpy(word, p - len, len); } if (*p != '\0') { p++; } } }
原文地址: http://www.cveoy.top/t/topic/cUZ1 著作权归作者所有。请勿转载和采集!