Rearrange a set of alphabets putting vowel infront follow by the rest

closed account (STR9GNh0)
how to rearrange a set of alphabets putting vowel in front follow by the rest of the remaining generated alphabets - i have created the rest of my program but i have zero knowledge on how to even get this started.

my characters are generated from:

srand (time (NULL));
int n = rand () % 26 + 65;
a [i] = static_cast <char> (n);

eg... i have generated from my compiler a set of alphabets (am not using string)

a h i j k l m n o p q r t a

i want to rearrange them into something like this

a i a o h j k l m n p q r t instead.

any help would be appreciated, thanks.
I'm not sure if this would work, but if you are using a string and not a char[], you can call the sort() stl algorithm, passing a predicate to it. The predicate is a function you define, which controls the criteria of the sort. It takes two arguments, each (in this case) would be char.
Basically, in the predicate you'd compare two chars. If you find that one is a vowel and the other isn't you automatically know that the first one is higher. Etc. Here's the documentation on the STL alg if you are interested: http://www.cplusplus.com/reference/algorithm/sort/
Anyway that's how I'd do it.

Try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <algorithm>
#include <cstring>
#include <iostream>

bool is_consonant(char ch) { return ch != 'a' && ch != 'e' && ch != 'i' && ch != 'o' && ch != 'u'; }

int main() {
    char abc[] = "abcdefghijklmnopqrstuvwxyz";
    std::remove_if(abc, abc+strlen(abc), &is_consonant);

    std::cout << abc;
}
closed account (STR9GNh0)
hi, thanks for the revert.

but how do i slot my function in the above?

eg. constructArray (a, MAXSIZE);

part 1 have generated a h i j k l m n o p q r t a

part 2 after sorting i want it to on the 2nd line display a i a o h j k l m n p q r t - the sorted with AEIOU infront.

i have done part 1 which will generate auto random alphabets now only left with part 2. i have no idea where i should slot it and compute it accordingly.

or rather by using a loop function to swap the array elements so that the vowels are on the left and the right are all the non-vowels.
Last edited on
You mean something like this?
1
2
3
void constructArray(char *arr, int size) {
    //generate 'size' characters into 'arr'
}

closed account (STR9GNh0)
yup. :) - i've done the generating in it le.

and have done a print to print out the first set of mixed characters in my output.
Last edited on
closed account (STR9GNh0)
anyone ?
what can't you do?
I gave you the function prototype, and the algorithm.
Your job is basically to copy-paste them together.
There are many ways of doing this....one way, given that you have filled a 26 element array randomly is:
1. declare two pointers p1,p2 to the array one to point to array[0] and one to traverse the array until it finds 'a'.
2. swap pointers
3.increment p2 to second element [1] and use p1 to find 'e'.........swap p1 and p2
4.repeat until 'a','e','i','o' and 'u' occupy the first five array elements.
5.sort array from element 5 to element 26 (inclusive)
Topic archived. No new replies allowed.