问题描述: 在给定字符串中找出单词( “单词”由大写字母和小写字母字符构成,其他非字母字符视为单词的间隔,如空格、问号、数字等等;另外单个字母不算单词);找到单词后,按照长度进行降序排序,(排序时如果长度相同,则按出现的顺序进行排列),然后输出到一个新的字符串中;如果某个单词重复出现多次,则只输出一次;如果整个输入的字符串中没有找到单词,请输出空串。输出的单词之间使用一个“空格”隔开,最后一个单词后不加空格。
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
void my_word(char input[],char output[])
{
char *p;
//char *temp1=(char *)malloc(sizeof(char)*10);
char *temp;
char *word[5];
//char *temp2=(char *)malloc(sizeof (char)*30);
int num[10]={0};
int num1[5]={0};
int num2[5]={0};
int len_input=strlen(input);
int i,j,k,m,max;
char except[] = ",";
char *blank = " ";
j=0;
k=0;
for (i=0;i<len_input;i++)
{
if (input[i]<'A' || (input[i]>'Z'&&input[i]<'a') || input[i]>'z')
{
input[i]=',';
num[k]=i;
k++;
}
}
/* num[k]=i;
for(j=0;j<=k;j++)
{
if(j==0)
num1[j]=num[j];
else
{
num1[j]=num[j]-num[j-1]-1;
}
}*/
j=0;
/*保存取出的单词*/
p= strtok(input,except);
while(NULL!=p)
{
word[j++]=p;
// printf("%s",p);
p= strtok(NULL,except);
}
/*对单词按照长度排序*/
for (i=0;i<5;i++)
{
for (j=i+1;j<5;j++)
{
if(strlen(word[i])<strlen(word[j]))
{
temp=word[j];
word[j]=word[i];
word[i]=temp;
}
}
}
/*删除相同单词*/
for (i=0;i<5;i++)
{
for(j=i+1;j<5;j++)
{
if(strcmp(word[i],word[j])==0)
word[j]="\0";
}
}
/*将单词连接起来输出*/
for (j=0;j<5;j++)
{
//temp=word[num2[j]];
if (0==j)
{
strncpy(output,word[j],strlen(word[j])+1);
}
else
{
strcat(output,blank);
strcat(output,word[j]);
}
}
return ;
}
int main()
{
char input[] ="abc,buses some lcal some";
char output[30];
my_word(input,output);
printf("%s",output);
printf("\n");
return 0;
}