Modify the string by swapping continuous vowels or consonants Last Updated : 15 Sep, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report Given a string str. The task is to modify the string by swapping two adjacent characters if both of them are vowels or both of them are consonants. Examples: Input: str = "geeksforgeeks" Output: geesfkogreesk The alphabets 'e' and 'e' in geeksforgeeks are vowels so they are swapped so the string becomes geeksforgeeks. The alphabets 'k' and 's' in geeksforgeeks are consonants so they are swapped so the string becomes geeskforgeeks. The alphabets 'k' and 'f' in geeskforgeeks are consonants so they are swapped so the string becomes geesfkorgeeks. The alphabets 'r' and 'g' in geesfkorgeeks are consonants so they are swapped so the string becomes geeskfogreeks. The alphabets 'e' and 'e' in geeskfogreeks are vowels so they are swapped so the string becomes geeskfogreeks. The alphabets 'k' and 's' in geeskfogreeks are vowels so they are swapped so the string becomes geeskfogreesk. Input:str = "gefeg" Output: gefeg No continuous vowels or consonants. Approach: Traverse through the characters in the string.Consider the current character and the next character.If both the characters are consonants or both the characters are vowels.Then swap the characters.Else continue the process till the end of the string. Below is the implementation of the above approach: C++ // C++ implementation of the above approach #include <bits/stdc++.h> using namespace std; // Function to check if a character is a vowel bool isVowel(char c) { c = tolower(c); if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') return true; return false; } // Function to swap two consecutively // repeated vowels or consonants string swapRepeated(string str) { // Traverse through the length of the string for (int i = 0; i < str.length() - 1; i++) { // Check if the two consecutive characters // are vowels or consonants if ((isVowel(str[i]) && isVowel(str[i + 1])) || (!isVowel(str[i]) && !isVowel(str[i + 1]))) // swap the two characters swap(str[i], str[i + 1]); } return str; } // Driver code int main() { string str = "geeksforgeeks"; cout << swapRepeated(str); return 0; } Java // Java implementation of the above approach class GFG { // Function to check if a // character is a vowel static boolean isVowel(char c) { c = Character.toLowerCase(c); if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') { return true; } return false; } // Function to swap two consecutively // repeated vowels or consonants static String swapRepeated(char str[]) { // Traverse through the // length of the string for (int i = 0; i < str.length - 1; i++) { char c = 0; // Check if the two consecutive characters // are vowels or consonants if ((isVowel(str[i]) && isVowel(str[i + 1])) || (!isVowel(str[i]) && !isVowel(str[i + 1]))) { // swap the two characters c = str[i]; str[i] = str[i + 1]; str[i + 1] = c; } } return String.valueOf(str); } // Driver code public static void main(String[] args) { String str = "geeksforgeeks"; System.out.println(swapRepeated(str.toCharArray())); } } // This code is contributed by 29AjayKumar Python 3 # Python3 implementation of the above approach # Function to check if a character is a vowel def isVowel(c) : c = c.lower(); if (c == 'a' or c == 'e' or c == 'i' or c == 'o' or c == 'u') : return True; return False; # Function to swap two consecutively # repeated vowels or consonants def swapRepeated(string) : # Traverse through the length of the string for i in range(len(string) - 1) : # Check if the two consecutive characters # are vowels or consonants if ((isVowel(string[i]) and isVowel(string[i + 1])) or (not(isVowel(string[i])) and not(isVowel(string[i + 1])))) : # swap the two characters (string[i], string[i + 1]) = (string[i + 1], string[i]); string = "".join(string) return string; # Driver code if __name__ == "__main__" : string = "geeksforgeeks"; print(swapRepeated(list(string))); # This code is contributed by Ryuga C# // C# implementation of the above approach using System; class GFG { // Function to check if a // character is a vowel static bool isVowel(char c) { c = char.ToLower(c); if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') { return true; } return false; } // Function to swap two consecutively // repeated vowels or consonants static String swapRepeated(char []str) { // Traverse through the // length of the string for (int i = 0; i < str.Length - 1; i++) { char c = (char)0; // Check if the two consecutive characters // are vowels or consonants if ((isVowel(str[i]) && isVowel(str[i + 1])) || (!isVowel(str[i]) && !isVowel(str[i + 1]))) { // swap the two characters c = str[i]; str[i] = str[i + 1]; str[i + 1] = c; } } return String.Join("",str); } // Driver code public static void Main(String[] args) { String str = "geeksforgeeks"; Console.WriteLine(swapRepeated(str.ToCharArray())); } } /* This code contributed by PrinciRaj1992 */ PHP <?php // PHP implementation of the above approach // Function to check if a character is a vowel function isVowel($c) { $c = strtolower($c); if ($c == 'a' || $c == 'e' || $c == 'i' || $c == 'o' || $c == 'u') return true; return false; } // Function to swap two consecutively // repeated vowels or consonants function swapRepeated($str) { // Traverse through the length of the string for ($i = 0; $i < strlen($str) - 1; $i++) { // Check if the two consecutive characters // are vowels or consonants if ((isVowel($str[$i]) && isVowel($str[$i + 1])) || (!isVowel($str[$i]) && !isVowel($str[$i + 1]))) { // swap the two characters $t = $str[$i]; $str[$i]= $str[$i + 1]; $str[$i+1] = $t; } } return $str; } // Driver code $str = "geeksforgeeks"; echo swapRepeated($str); return 0; // This code is contributed by ChitraNayal ?> JavaScript <script> // JavaScript implementation of the above approach // Function to check if a // character is a vowel function isVowel(c) { c = c.toLowerCase(); if (c === "a" || c === "e" || c === "i" || c === "o" || c === "u") { return true; } return false; } // Function to swap two consecutively // repeated vowels or consonants function swapRepeated(str) { // Traverse through the // length of the string for (var i = 0; i < str.length - 1; i++) { // Check if the two consecutive characters // are vowels or consonants if ( (isVowel(str[i]) && isVowel(str[i + 1])) || (!isVowel(str[i]) && !isVowel(str[i + 1])) ) { // swap the two characters var c = str[i]; str[i] = str[i + 1]; str[i + 1] = c; } } return str.join(""); } // Driver code var str = "geeksforgeeks"; document.write(swapRepeated(str.split(""))); </script> Outputgeesfkogreesk Complexity Analysis: Time Complexity: O(N) for traversing the given string str.Auxiliary Space: O(1) as constant space is used. Comment More infoAdvertise with us Next Article C program to count number of vowels and consonants in a String S SanjayR Follow Improve Article Tags : Strings Technical Scripter DSA vowel-consonant Practice Tags : Strings Similar Reads C program to count number of vowels and consonants in a String Given a string and write a C program to count the number of vowels and consonants in this string. Examples: Input: str = "geeks for geeks" Output: Vowels: 5 Consonants: 8 Input: str = "abcdefghijklmnopqrstuvwxyz" Output: Vowels: 5 Consonants: 21Using For LoopsTake the string as inputTake each charac 4 min read Modify the string such that it contains all vowels at least once Given a string S containing only Uppercase letters, the task is to find the minimum number of replacement of characters needed to get a string with all vowels and if we cannot make the required string then print Impossible. Examples: Input: str = "ABCDEFGHI"Output: AOUDEFGHIExplanation: There are al 11 min read Count of possible Strings by replacing consonants with nearest vowel Given a string str consisting of N letters, the task is to find the total number of strings that can be generated by replacing each consonant with the vowel closest to it in the English alphabet. Examples: Input: str = "code"Output: 2Explanation: Str = "code" has two consonant c and d. Closest vowel 4 min read Print all Substrings of a String that has equal number of vowels and consonants Given a string S, the task is to print all the substrings of a string that has an equal number of vowels and consonants. Examples: Input: "geeks"Output: "ge", "geek", "eeks", "ek" Input: "coding"Output: "co", "codi", "od", "odin", "di", "in" Naive Approach: The basic approach to solve this problem i 13 min read Count the pairs of vowels in the given string Given a string str consisting of lowercase English alphabets, the task is to count the number of adjacent pairs of vowels.Examples: Input: str = "abaebio" Output: 2 (a, e) and (i, o) are the only valid pairs.Input: str = "aeoui" Output: 4 Approach: Starting from the first character of the string to 5 min read Minimum cost to convert given string to consist of only vowels Given string str of lower case alphabets, the task is to find the minimum cost to change the input string in a string that contains only vowels. Each consonant is changed to the nearest vowels. The cost is defined as the absolute difference between the ASCII value of consonant and vowel. Examples: I 4 min read Like