Alternate vowel and consonant string in C++

C++Server Side ProgrammingProgramming

In case of a given string, rearrange characters of the given string so that the vowels and consonants occupy alternate position. If string cannot be rearranged in proper way, display “no such string”. Order of vowels with respect to each other and the order of consonants with respect to each other should be preserved.

If more than one needed strings can be constructed, display the lexicographically smaller.


Input : Tutorial
Output : Tutorila
Input : onse
Output : nose

There exist two possible outcomes "nose" and "ones". Since "nose" is lexicographically smaller, we display it.

  • Number of vowels and consonants in given string is counted.

  • In this case, if difference between counts is more than one, return “Not Possible”.

  • In this case, if there are more vowels than consonants, display first vowel first and recur for remaining string.

  • In this case, if there are more consonants than vowels, display first consonant first and recur for remaining string.

  • In this case, if counts are same, compare first vowel with first consonant and display the smaller one first.


 Live Demo

// C++ application of alternate vowel and consonant string
#include <bits/stdc++.h>
using namespace std;
// 'ch1' is treated as vowel or not
bool isVowel(char ch1){
   if (ch1 == 'a' || ch1 == 'e' || ch1 == 'i' ||
   ch1 == 'o' || ch1 =='u')
   return true;
   return false;
// build alternate vowel and consonant string
// str1[0...l2-1] and str2[start...l3-1]
string createAltStr(string str1, string str2,
int start1, int l1){
   string finalStr1 = "";
   // first adding character of vowel/consonant
   // then adding character of consonant/vowel
   for (int i=0, j=start1; j<l1; i++, j++)
   finalStr1 = (finalStr1 + +;
   return finalStr1;
// function to locate or find the needed alternate vowel and consonant string
string findAltStr(string str3){
   int nv1 = 0, nc1 = 0;
   string vstr1 = "", cstr1 = "";
   int l1 = str3.size();
   for (int i=0; i<l1; i++){
      char ch1 =;
      // count vowels and updaye vowel string
      if (isVowel(ch1)){
      vstr1 = vstr1 + ch1;
   // counting consonants and updating consonant string
         cstr1 = cstr1 + ch1;
   // no such string can be built
   if (abs(nv1-nc1) >= 2)
   return "no such string";
   // delete first character of vowel string
   // then built alternate string with
   // cstr1[0...nc1-1] and vstr1[1...nv1-1]
   if (nv1 > nc1)
   return ( + createAltStr(cstr1, vstr1, 1, nv1));
   // delete first character of consonant string
   // then built alternate string with
   // vstr1[0...nv1-1] and cstr1[1...nc1-1]
   if (nc1 > nv1)
   return ( + createAltStr(vstr1, cstr1, 1, nc1));
   // if both vowel and consonant
   // strings are of equal length
   // start building string with consonant
   if ( <
   return createAltStr(cstr1, vstr1, 0, nv1);
   // start building string with vowel
   return createAltStr(vstr1, cstr1, 0, nc1);
 // Driver program to test above
int main(){
   string str3 = "Tutorial";
   cout<< findAltStr(str3);
   return 0;



Time Complexity &minus O(n), where ‘n’ is treated as the length of the string

Published on 29-Jan-2020 07:47:42