找出字符串中的最长单词并输出:
#include <stdio.h>
#include <string.h>
int main(int argc, const char * argv[]) {
char str[] = "adventure may hurt you but monotony will kill you";
char *p;
int count = 0;
int maxlen = 0;
int lenth = 0;
int maxIndex = 0;
for ( p = str ; *p!= '\0' ; p++) {
if (*p != ' ') {
lenth++;
}else{
if (lenth >= maxlen) {
maxlen = lenth;
maxIndex = count;
lenth = 0;
}
}
count++;
}
for (int i = 0; i < maxlen; i++) {
printf("%c",str[maxIndex-maxlen+i]);
}
return 0;
}
if (lenth >= maxlen) {
}