Convert vowels into upper case character in a given string

27 Apr, 2021
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: GEEksfOrGEEksExplanation: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++
Java
Python3
C#
Javascript
// C++ program to implement// the above approach #include <bits/stdc++.h>using namespace std; // Function to convert vowels// into uppercasestring 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 Codeint main(){    string str = "eutopia";    cout << conVowUpp(str);}
// Java program to implement// the above approachimport java.util.*;class GFG{ // Function to convert vowels// into uppercasestatic 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 Codepublic static void main(String[] args){  String str = "eutopia";  conVowUpp(str.toCharArray());}} // This code is contributed by 29AjayKumar
# Python3 program to implement# the above approach # Function to convert vowels# into uppercasedef 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 Codeif __name__ == '__main__':         str = "eutopia"         conVowUpp(str) # This code is contributed by Amit Katiyar
// C# program to implement// the above approachusing System;class GFG{ // Function to convert vowels// into uppercasestatic 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 Codepublic static void Main(String[] args){  String str = "eutopia";  conVowUpp(str.ToCharArray());}} // This code is contributed by shikhasingrajput
<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)
29AjayKumar
shikhasingrajput
amit143katiyar
simmytarika5
rdtank
School Programming
Searching
Strings
Searching
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.