Convert vowels into upper case character in a given string
Given string str, the task is to convert all the vowels present in the given string to uppercase characters in the given string.
Examples:
Input: str = “GeeksforGeeks”
Output: GEEksfOrGEEks
Explanation:
Vowels present in the given string are: {‘e’, ‘o’}
Replace ‘e’ to ‘E’ and ‘o’ to ‘O’.
Therefore, the required output is GEEksfOrGEEks.Input: str = “eutopia”
Output: EUtOpIA
Approach: The idea is to iterate over the characters of the given string and check if the current character is a lowercase character and a vowel or not. If found to be true, convert the character to its uppercase. Follow the steps below to solve the problem:
- Traverse the given string and check if str[i] is a lowercase vowel or not.
- If found to be true, replace str[i] to (str[i] – ‘a’ + ‘A’).
- Finally, after complete the traversal of the string, print the final string.
Below is the implementation of the approach:
C++
// C++ program to implement // the above approach #include <bits/stdc++.h> using namespace std; // Function to convert vowels // into uppercase string conVowUpp(string& str) { // Stores the length // of str int N = str.length(); for ( int i = 0; i < N; i++) { if (str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u' ) { str[i] = str[i] - 'a' + 'A' ; } } return str; } // Driver Code int main() { string str = "eutopia" ; cout << conVowUpp(str); } |
Java
// Java program to implement // the above approach import java.util.*; class GFG{ // Function to convert vowels // into uppercase static void conVowUpp( char [] str) { // Stores the length // of str int N = str.length; for ( int i = 0 ; i < N; i++) { if (str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u' ) { char c = Character.toUpperCase(str[i]); str[i] = c; } } for ( char c : str) System.out.print(c); } // Driver Code public static void main(String[] args) { String str = "eutopia" ; conVowUpp(str.toCharArray()); } } // This code is contributed by 29AjayKumar |
Python3
# Python3 program to implement # the above approach # Function to convert vowels # into uppercase def conVowUpp( str ): # Stores the length # of str N = len ( str ) str1 = "" for i in range (N): if ( str [i] = = 'a' or str [i] = = 'e' or str [i] = = 'i' or str [i] = = 'o' or str [i] = = 'u' ): c = ( str [i]).upper() str1 + = c else : str1 + = str [i] print (str1) # Driver Code if __name__ = = '__main__' : str = "eutopia" conVowUpp( str ) # This code is contributed by Amit Katiyar |
C#
// C# program to implement // the above approach using System; class GFG{ // Function to convert vowels // into uppercase static void conVowUpp( char [] str) { // Stores the length // of str int N = str.Length; for ( int i = 0; i < N; i++) { if (str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u' ) { char c = char .ToUpperInvariant(str[i]); str[i] = c; } } foreach ( char c in str) Console.Write(c); } // Driver Code public static void Main(String[] args) { String str = "eutopia" ; conVowUpp(str.ToCharArray()); } } // This code is contributed by shikhasingrajput |
Javascript
<script> // JavaScript program to implement // the above approach // Function to convert vowels // into uppercase function conVowUpp(str) { // Stores the length // of str var N = str.length; for ( var i = 0; i < N; i++) { if ( str[i] === "a" || str[i] === "e" || str[i] === "i" || str[i] === "o" || str[i] === "u" ) { document.write(str[i].toUpperCase()); } else { document.write(str[i]); } } } // Driver Code var str = "eutopia" ; conVowUpp(str); </script> |
EUtOpIA
Time Complexity: O(N)
Auxiliary Space: O(1)