作者在 2012-10-28 18:19:25 发布以下内容
//:字符串中最长单词
#include <stdio.h>
int main(){
char array[100], c;
int word = 0, max = 0;
int i, cnt = 0, flag;
gets(array);
for(i=0; (c=array[i])!=''; i++){
if(' '==c){
word = 0;
}else if(0==word){
word = 1;
cnt = 1;
}else if(1==word){
++cnt;
if(cnt>max){ //记录最长单词长度
max = cnt;
flag = i; //记住最长单词结束位置
}
}
}//for
for(i=flag-max+1; i<=flag; i++){
printf("%c", array[i]);
}
printf("n");
return 0;
}
#include <stdio.h>
int main(){
char array[100], c;
int word = 0, max = 0;
int i, cnt = 0, flag;
gets(array);
for(i=0; (c=array[i])!=''; i++){
if(' '==c){
word = 0;
}else if(0==word){
word = 1;
cnt = 1;
}else if(1==word){
++cnt;
if(cnt>max){ //记录最长单词长度
max = cnt;
flag = i; //记住最长单词结束位置
}
}
}//for
for(i=flag-max+1; i<=flag; i++){
printf("%c", array[i]);
}
printf("n");
return 0;
}