第2个回答 2022-05-24
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 500
int gecifun(char *geci, char *strmax, char *strmin)
{
char temp[30];//存储每个单词数组
int num=0,i,word=0;//单词数量和每个单词数组下标
for(i=0;(geci[i])!='\0';i++)
{
if(geci[i]==' '||geci[i]==',' || geci[i]=='.' || geci[i]=='?') //单词分隔符
{
if (word != 0){
temp[word]='\0';
if (num==1){//获取第一个单词的时候,将第一个单词复制给最大和最小
strcpy(strmax,temp);
strcpy(strmin,temp);
}
else//第二个单词之后比较字符串长度之后进行复制
{
if (strlen(temp)>=strlen(strmax))
strcpy(strmax,temp);
if (strlen(temp)<=strlen(strmin))
strcpy(strmin,temp);
}
num++;//单词数量递增
word = 0;//单词数组下标置零
}
}
else
temp[word++] = geci[i];//每个单词按位置赋值
}
return num;
}
int main()
{
int acount;
char lstr[30],sstr[30];
char song[N] = "Daddy finger, Daddy finger, where are you? Here I am. Here I am."
"How do you do? Mammy finger, Mammy finger, where are you? Here I am. Here "
"I am. How do you do? Brother finger, Brother finger, where are you? Here "
"I am. Here I am. How do you do? Sister finger, Sister finger, where are you?"
"Here I am. Here I am. How do you do? Baby finger, Baby finger, where are you?"
"Here I am. Here I am. How do you do?";
acount=gecifun(song,lstr,sstr);
printf("歌词中共有%d个单词。歌词中最长的单词为%s,最短的单词为%s。\n",acount,lstr,sstr);
return 0;
}