#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void MaxWord(const char *str)
{
char *maxstr=(char *)malloc((strlen(str)+1)*sizeof(char));//存储最长单词
char *curstr=(char *)malloc((strlen(str)+1)*sizeof(char));//存储最短单词
int max=0;//最长单词的长度
int cur=0;//当前单词的长度
while(*str!='\0')
{
if(isalpha(*str))
{
curstr[cur++]=*str;
}
else
{
if(cur>max)//将新的单词,更新到maxstr中
{
curstr[cur]='\0';//使curstr成为字符串
strcpy(maxstr,curstr);
max=cur;
}
cur=0;//丢弃之前的单词,为新单词做准备
}
str++;
}
printf("%s\n",maxstr);
free(curstr);
free(maxstr);
}
int main()
{
char *str[3]={"china","japan","england"};
printf("%s\n",str);
return 0;
}
写一个函数,输入一行字符,将此字符串中最长的单词输出
最新推荐文章于 2024-11-24 11:25:59 发布
本文介绍如何编写一个函数,该函数接收一串字符作为输入,分析其中的单词,并返回最长的那个单词。通过实例代码详细解析实现过程。
4632

被折叠的 条评论
为什么被折叠?



