
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Alternate vowel and consonant string in C/C++?
There is a string given, rearrange characters in the string such that the vowels and consonants occupy the alternate position. If string can’t be rearranged in the above manner, print “not possible”.
The order of vowels with respect to each other and the order of consonants with respect to each other should be maintained.
Input: abce Output: abec
Explanation
Find the number of vowels and consonants in the string.
If the difference between no. of vowels and consonants are more than one, return “Not Possible”.
If there is a condition that more vowels are present in the string than consonants, print the first vowel first and recur for the remaining string.
If there is a condition that more consonants present in the string than vowels, print the first consonant first and recur for the remaining string.
If no. of vowels and consonants are same, compare the first vowel with the first consonant and print the smaller one first
Example
#include <iostream> using namespace std; bool isVowel(char ch) { if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch =='u') return true; return false; } string createAltStr(string str1, string str2, int start, int l) { string finalStr = ""; for (int i=0, j=start; j<l; i++, j++) finalStr = (finalStr + str1.at(i)) + str2.at(j); return finalStr; } string findAltStr(string str) { int nv = 0, nc = 0; string vstr = "", cstr = ""; int l = str.size(); for (int i=0; i<l; i++) { char ch = str.at(i); if (isVowel(ch)) { nv++; vstr = vstr + ch; } else { nc++; cstr = cstr + ch; } } if (abs(nv-nc) >= 2) return "no such string"; if (nv > nc) return (vstr.at(0) + createAltStr(cstr, vstr, 1, nv)); if (nc > nv) return (cstr.at(0) + createAltStr(vstr, cstr, 1, nc)); if (cstr.at(0) < vstr.at(0)) return createAltStr(cstr, vstr, 0, nv); return createAltStr(vstr, cstr, 0, nc); } int main() { string str = "abde"; cout << findAltStr(str); return 0; }
- Related Articles
- Alternate vowel and consonant string in 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++
- C++ Program to Check Whether a character is Vowel or Consonant
- Java program to find whether given 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
- Alternate Lower Upper String Sort in C++
- 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
- Deleting the last vowel from a string in JavaScript
- Number of flips to make binary string alternate - Set 1 in C++
- Count the nodes of the tree whose weighted string contains a vowel in C++
