• 将一个字符串的元音字母拷贝到还有一个字符串,并排序


    问题描写叙述:
    有一字符串。里面可能包括英文字母(大写、小写)、数字、特殊字符,如今须要实现一函数。将此字符串中的元音字母挑选出来,存入还有一个字符串中,并对字符串中的字母进行从小到大的排序(小写的元音字母在前,大写的元音字母在后,依次有序)。
    说明:
    1、元音字母是a,e,i,o,u,A,E,I,O,U
    2、筛选出来的元音字母,不须要剔重
     
    终于输出的字符串,小写元音字母排在前面,大写元音字母排在后面,依次有序。


     
    要求实现函数:
    void sort_vowel (char* input, char* output);
    输入:char* input,表示输入的字符串

    输出:char* output,排好序之后的元音字符串


    C代码例如以下:

    #include<stdio.h>
    #include<stdlib.h>
    #include<ctype.h>
    #include<string.h>
    
    int cmp(const void* a, const void* b);
    int is_vowel(char a);
    void sort_vowel(char* input, char* output);
    
    int main()
    {
    	char input[1000] = {''};
    	char output[200] = {''};
    
    	printf("please input the original str: ");
    	gets(input);
    
    	printf("the sorted vowel is: ");
    	sort_vowel(input,output);
    	return 0;
    }
    
    int cmp(const void* a, const void* b)
    {
    	return *(char*)a - *(char*)b;
    }
    
    int is_vowel(char a)
    {
    	if(a=='a' || a=='e' || a=='i' || a=='o' || a=='u' ||
    		a=='A' || a=='E' || a=='I' || a=='O' || a=='U')
    		return 1;
    	else
    		return 0;
    }
    
    void sort_vowel(char* input, char* output)
    {
    	char low[100] = {''};
    	char up[100] ={''};
    	int low_index=0;
    	int up_index=0;
    	while(*input != '')
    	{
    		if(is_vowel(*input))
    		{
    			if(isupper(*input))
    			{
    				up[up_index++] = *input;
    				input++;
    				continue;
    			}
    			else
    			{
    				low[low_index++] = *input;
    				input++;
    				continue;
    			}
    		}
    		input++;
    	}
    	qsort(up,strlen(up),sizeof(char),cmp);
    	qsort(low,strlen(low),sizeof(char),cmp);
    	strcat(low,up);
    	strcpy(output,low);
    	while(*output != '')
    		printf("%c",*output++);
    	printf("
    ");
    }

    几组測试用比例如以下所看到的:



  • 相关阅读:
    OpenCV 之 图像平滑
    C++11 之 nullptr
    C++11 之 scoped enum
    C++ 之 常量成员函数
    德国的挑战
    OpenCV 之 直方图处理
    OpenCV 之 霍夫变换
    排序算法之——归并排序(两种方法及其优化)
    排序算法之——快速排序(剖析)
    排序算法之——桶排序
  • 原文地址:https://www.cnblogs.com/mthoutai/p/6788881.html
Copyright © 2020-2023  润新知