C语言寻找长度最长的单词
时间: 2024-01-19 21:03:39 浏览: 56
以下是C语言寻找长度最长的单词的代码实现:
```c
#include <stdio.h>
#include <string.h>
void findLongestWord(char sentence[]) {
int i, length, max = 0, start = 0, end = 0;
length = strlen(sentence);
for (i = 0; i < length; i++) {
if (sentence[i] == ' ') {
if (i - start > max) {
max = i - start;
end = i - 1;
}
start = i + 1;
}
}
if (length - start > max) {
max = length - start; end = length - 1;
}
printf("The longest word is: ");
for (i = end - max + 1; i <= end; i++) {
printf("%c", sentence[i]);
}
}
int main() {
char sentence[100];
printf("Please enter a sentence: ");
gets(sentence);
findLongestWord(sentence);
return 0;
}
```