-2

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;
}
Cloud
  • 18,753
  • 15
  • 79
  • 153
XWind
  • 1
  • 3
  • Lookup bubble sort. It could handle what your looking to do. – Bioto Jun 10 '14 at 15:59
  • 1
    Sorting is easy with the standard library [`qsort()`](http://linux.die.net/man/3/qsort) function. – unwind Jun 10 '14 at 16:01
  • 1
    @NicholasYoung - Any *stable* sorting algorithm will do, but given that this seems like a learning program and not a production one, bubble sort or insertion sort would both be appropriate. – Mr. Llama Jun 10 '14 at 16:03
  • 1
    Probably your class covered sorting algorithms and you are supposed to use one of those. – sth Jun 10 '14 at 16:03
  • I know how to sort an array of integers with bubble sort, but I don't know how to sort words in a string based on their vowel number with it. – XWind Jun 10 '14 at 16:06
  • If you show the code you have done so far then it usually easier for people here to give you better suggestions with code examples – Deepend Jun 10 '14 at 16:20
  • You can access each character of a string as if it were an array. You can iterate through each character in the string and use character comparison to figure out what you need to do. There's other string manipulation functions you can use in the C library that will make this job easier... you can look them up. [This one](http://stackoverflow.com/questions/9210528/split-string-with-delimiters-in-c) might give you a clue... – Ryan J Jun 10 '14 at 16:25
  • Currently I have this: http://pastebin.com/vTXJ3Cp5 – XWind Jun 10 '14 at 16:36
  • Paste any code and/or output directly into the question... – Ryan J Jun 10 '14 at 16:37
  • I updated the question with my current code. – XWind Jun 10 '14 at 16:41
  • @xwind, instead of separate `string1` `string2`, go ahead and set up your structure and read the strings directly into the array of structs. Then you will just need to process the structures and fill in the number of vowels in each before sorting. Remember, you must allocate for the string within a struct, so when entering the words, just use strdup to copy the string to the structure. e.g. `vc[idx].word = strdup (string1); idx++;` `strdup` allocates for you with malloc, so you will want to `free(vc[idx].word);` to be correct (or just exit and it is freed for you :) – David C. Rankin Jun 10 '14 at 18:09
  • You have a fully working example, btw. – Cloud Jun 12 '14 at 18:38

1 Answers1

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.

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
  • I haven't worked with an array of structs yet, but I will try. Thx. – XWind Jun 10 '14 at 16:59
  • Care would need to be taken to preserve the original location the word appeared in, otherwise, solid idea. – Ryan J Jun 10 '14 at 17:04
  • @XWind - there are no tricks. It will be the same as working with a single dimensional array. If you know the maximum number of words you are dealing with, then you can just declare the number of structures you need directly. E.g. `struct vowelcnt vc[100];`. You will then access the struct members with `dot` notation, e.g. `vc.word = wordpointer;`, `vc.numvowels = x;`. Or you can declare a number of pointers to struct `struct vowelcnt *vc[100];` Here you would need to allocate with malloc and access with rt-arrow notation, e.g. `vc->word = wordpointer;` – David C. Rankin Jun 10 '14 at 17:56