写一个函数,输入一行字符 ,将此字符串中最长的单词输出
这题怎么写??
有人知道吗?
#include <stdio.h> /* 输出字符串中最长的单词 */
#include <string.h>
#include <stdlib.h>
#define MAXSIZE 80
int main(void)
{
char string[MAXSIZE], longest_word[MAXSIZE];
int liv_a, i, word_len, cur_len;
printf("Enter string:\n");
for (word_len = 0, cur_len = 0, i = 0; i < MAXSIZE - 1; i++)
{
liv_a = getchar();
if (liv_a == '\n')
{
break;
}
if (liv_a != ' ')
{
string[cur_len++] = liv_a;
}
else if(cur_len == word_len || cur_len < word_len)
{
cur_len = 0;
}
else
{
if (cur_len > word_len)
{
string[cur_len] = '\0';
word_len = cur_len;
strcpy(longest_word, string);
cur_len = 0;
}
}
}
if (liv_a == '\n' && cur_len > word_len)
{
string[cur_len] = '\0';
strcpy(longest_word, string);
}
printf("longest_word = %s\n", longest_word);
exit(0);
}