华为软件类上机测试题

来源:互联网 发布:js获取文本框输入的值 编辑:程序博客网 时间:2024/04/27 16:46
字符串处理转换
问题描述:    
在给定字符串中找出单词( “单词”由大写字母和小写字母字符构成,其他非字母字符视为单词的间隔,如空格、问号、数字等等;另外单个字母不算单词);找到单词后,按照长度进行降序排序,(排序时如果长度相同,则按出现的顺序进行排列),然后输出到一个新的字符串中;如果某个单词重复出现多次,则只输出一次;如果整个输入的字符串中没有找到单词,请输出空串。输出的单词之间使用一个“空格”隔开,最后一个单词后不加空格。
要求实现函数:
void my_word(charinput[], char output[])
【输入】  char input[], 输入的字符串
【输出】  char output[],输出的字符串
【返回】 无
示例
输入:charinput[]="some local buses, some1234123drivers" ,
输出:charoutput[]="drivers local buses some"
输入:charinput[]="%A^123 t 3453i*()" ,

输出:charoutput[]=""

实现代码如下:

void convert(char input[],char output[]){if(input==NULL||output==NULL)return;char *out=output;int len=strlen(input);//输入字符数组的长度char *temp[10];for(int j=0;j<10;j++)temp[j]=(char*)malloc(sizeof(char)*10);char *word[10];for(int j=0;j<10;j++)word[j]=temp[j];int i=0;int flag=0;int word_index=0;while(*(input+i)!='\0'){if((*(input+i) >= 'a' && *(input+i) <= 'z') || (*(input+i) >= 'A' && *(input+i) <= 'Z')){*word[word_index]++=*(input+i);flag=1;}else if(flag==1){*word[word_index]='\0';word_index++;//另外一个单词flag=0;}i++;}*word[word_index]='\0';char *tempp;for(int j=0;j<word_index;j++)for(i=0;i<word_index-j;i++){if(strlen(temp[i])>1)//单词长度>1{if(strlen(temp[i])<strlen(temp[i+1])){tempp=temp[i];temp[i]=temp[i+1];temp[i+1]=tempp;}}}for(i=0;i<word_index+1;i++){if(strcmp(temp[i],temp[i+1])&&strlen(temp[i])>1){for(int j=0;j<strlen(temp[i]);j++)*out++=*(temp[i]+j);*out++=0x20;}}*out='\0';}


0 0