在一个字符串排序的话根据自己的元音数量 [英] Sort words in a string based on their vowel number

查看:160
本文介绍了在一个字符串排序的话根据自己的元音数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在C,它允许用户说,他们希望有多少个字,字符串输入写一个程序,然后我必须根据自己的元音数量(有更多的元音单词的那些话排序是第一和一个与最少的元音是上 - 如果两个词有相同数量的元音然后让它们的顺序,因为他们已经出现了)。例如:

字符串 - AAAA BBBBBBB乙CD本地AAA BB一捅

排序字符串 - AAAA Aaa级家用捅了BBBBBBB乙CD BB

我知道怎么写的第一部分,但排序一部分,我不知道。有人可以帮助我与好吗?

编辑:目前我有这个code:

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&string.h中GT;
#包括LT&;&文件ctype.h GT;
#pragma警告(禁用:4996)诠释主(){
    字符字符串1 [20],字符串2 [100] = {'\\ 0'};
    INT N,I;
    做{
        的printf(请输入你想要的字符串中输入的字数:);
        scanf函数(%d个,&安培; N);
        如果(N 2){
            的printf(你必须至少输入两个词\\ n);
            的printf(请输入你想要的字符串中输入的字数:);
            scanf函数(%d个,&安培; N);
        }
    }而(N 2);    对于(i = 0; I< N;我++){
        的printf(请输入一个字:);
        scanf函数(%[^ \\ n],字符串1);
        如果(strlen的(字符串2)== 0)
            的strcpy(字符串2,字符串1);
        其他{
            strcat的(字符串2,);
            strcat的(字符串2,字符串1);
        }
    }    的printf(%S \\ n,字符串2);    返回0;
}


解决方案

您将要创建一个包含一个指针的字,然后在每个单词的元音数量结构的数组。是这样的:

 结构vowelcnt {
    字符*字;
    INT numvowels;
}

然后结构数组(根据 numvowels 降序排列。然后,只需通过分类结构输出字循环排序这将使你在元音数量排序的话包含的。希望有所帮助。

I must write a program in C that allows an user to say how many words they want to enter in a string and then I must sort those words based on their vowel number(the word with more vowels is first and the one with the least vowels is last - if two words have the same number of vowels then leave them in the order as they have appeared). For example:

string - "Aaaa Bbbbbbb B CD home Aaa BB A poke"

Sorted string - "Aaaa Aaa home poke A Bbbbbbb B CD BB"

I know how to write the first part, but for the sort part I have no idea. Can someone help me with that please?

EDIT: Currently I have this code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#pragma warning (disable: 4996)

int main(){
    char string1[20], string2[100] = { '\0' };
    int N, i;
    do{
        printf("Enter the number of words you want to enter in the string: ");
        scanf("%d", &N);
        if (N < 2){
            printf("You must enter at least two words.\n");
            printf("Enter the number of words you want to enter in the string: ");
            scanf("%d", &N);
        }
    } while (N < 2);

    for (i = 0; i < N; i++){
        printf("Enter a word: ");
        scanf(" %[^\n]", string1);
        if (strlen(string2) == 0)
            strcpy(string2, string1);
        else {
            strcat(string2, " ");
            strcat(string2, string1);
        }
    }

    printf("%s\n", string2);

    return 0;
}

解决方案

You will want to create an array of structs that hold a pointer to the word and then the number of vowels in each word. Something like:

struct vowelcnt {
    char *word;
    int numvowels;
}

Then sort the array of structs (descending order based on numvowels. Then simply loop through the sorted structs outputting the word which would give you the words sorted in order of the number of vowels contained. Hope that helps.

这篇关于在一个字符串排序的话根据自己的元音数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆