
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Alternate vowel and consonant string in C++
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.
Examples
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.
Example
// 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 + str1.at(i)) + str2.at(j); 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 = str3.at(i); // count vowels and updaye vowel string if (isVowel(ch1)){ nv1++; vstr1 = vstr1 + ch1; } // counting consonants and updating consonant string else{ nc1++; 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 (vstr1.at(0) + 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 (cstr1.at(0) + createAltStr(vstr1, cstr1, 1, nc1)); // if both vowel and consonant // strings are of equal length // start building string with consonant if (cstr1.at(0) < vstr1.at(0)) 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; }
Output
Tutorila
Time Complexity &minus O(n), where ‘n’ is treated as the length of the string
- Related Articles
- Alternate vowel and consonant string in C/C++?
- Vowel, other characters and consonant difference in a string JavaScript
- Print all Subsequences of String which Start with Vowel and End with Consonant in C++
- Program to find if a character is vowel or Consonant in C++
- Java program to find whether given character is vowel or consonant
- C++ Program to Check Whether a character is Vowel or Consonant
- Java Program to Check Whether an Alphabet is Vowel or Consonant
- Haskell Program to Check Whether an Alphabet is Vowel or Consonant
- Java program to find whether given character is vowel or consonant using switch case
- Alternate casing a string in JavaScript
- Distance to nearest vowel in a string - JavaScript
- Alternate Lower Upper String Sort in C++
- Deleting the last vowel from a string in JavaScript
- Python Program to accept string starting with vowel
- Java program to check occurence of each vowel in String
