
- 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
Alternating Vowels and Consonants in C/C++
Given an input string with vowels and consonants. Rearrange the string such a way that vowels and consonants occupies alternate position in final string. As we are arranging vowels and consonants at alternate position the input string must satisfy either of the following conditions −
Number of vowels and consonants must be same e.g. string "individual" has 5 vowels and 5 consonants.
If number of vowels are more, then difference between number of vowels and number of consonants must be 1 e.g. string "noe" has 2 vowels and 1 consonant.
If number of consonants are more, then difference between number of consonants and number of vowels must be 1 e.g. string "objective" has 4 vowels and 5 consonants.
Algorithm
1. count number of vowels 2. Count number of consonants 3. if difference between number of vowels and consonants or viceversa is greater than 1 then return error 4. Split input string into two parts: a) First string contains only vowels b) Second string contains only consonants 5. If number of consonants and vowels are equal, then create final string by picking a character from each string alternatively. 6. If number of vowels are greater than consonants, then: a) Include additional vowel in final string to make both strings of equal length b) Create final string by appending a character from each string alternatively 7. If number of consonants are greater than vowels, then a) Include additional consonant in final string to make both strings of equal length b) Create final string by appending a character from each string alternatively
Example
#include <iostream> #include <string> using namespace std; bool is_vowel(char ch) { if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch =='u') { return true; } return false; } string create_final_string(string &s1, string &s2, int start, int end) { string final_string; for (int i = 0, j = start; j < end; ++i, ++j) { final_string = (final_string + s1.at(i)) + s2.at(j); } return final_string; } string create_alternate_string(string &s) { int vowel_cnt, consonant_cnt; string vowel_str, consonant_str; vowel_cnt = consonant_cnt = 0; for (char c : s) { if (is_vowel(c)) { ++vowel_cnt; vowel_str += c; } else { ++consonant_cnt; consonant_str += c; } } if (abs(consonant_cnt - vowel_cnt) >= 2) { cerr << "String cannot be formed with alternating vowels and cosonants\n"; exit(1); } if ((consonant_cnt - vowel_cnt) == 0) { return create_final_string(vowel_str, consonant_str, 0, vowel_cnt); } else if (vowel_cnt > consonant_cnt) { return vowel_str.at(0) + create_final_string(consonant_str,vowel_str, 1, vowel_cnt); } return consonant_str.at(0) + create_final_string(vowel_str,consonant_str, 1, consonant_cnt); } int main() { string s1 = "individual"; string s2 = "noe"; string s3 = "objective"; cout << "Input : " << s1 << "\n"; cout << "Output: " << create_alternate_string(s1) << "\n\n"; cout << "Input : " << s2 << "\n"; cout << "Output: " << create_alternate_string(s2) << "\n\n"; cout << "Input : " << s3 << "\n"; cout << "Output: " << create_alternate_string(s3) << "\n\n"; }
Output
When you compile and execute above code it will generate following output −
Input : individual Output: inidivudal Input : noe Output: one Input : objective Output: bojecitev
- Related Articles
- Validating alternating vowels and consonants in JavaScript
- Arrange consonants and vowels nodes in a linked list in C++?
- C# Program to count number of Vowels and Consonants in a string
- Frequency of vowels and consonants in JavaScript
- Moving vowels and consonants using JavaScript
- How to count number of vowels and consonants in a string in C Language?
- Replace All Consonants with Nearest Vowels In a String using C++ program
- C Program to count vowels, digits, spaces, consonants using the string concepts
- C++ Program to Find the Number of Vowels, Consonants, Digits and White Spaces in a String
- How to detect vowels vs consonants in Python?
- Java Program to Count the Number of Vowels and Consonants in a Sentence
- Swift Program to Count the Number of Vowels and Consonants in a Sentence
- Kotlin Program to Count the Number of Vowels and Consonants in a Sentence
- Haskell Program to Count the Number of Vowels and Consonants in a Sentence
- How to Count the Number of Vowels and Consonants in a Sentence in Golang?
