Open In App

Convert vowels into upper case character in a given string

Last Updated : 03 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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>


Output

EUtOpIA











Time Complexity: O(N)
Auxiliary Space: O(1)

Approach:

In this approach, we are using a range-based for loop and iterating over each character in the string. We then check if the character is a vowel, and if it is, we convert it to uppercase by subtracting 32 from its ASCII value. Finally, we return the modified string.

This approach is more concise and efficient than the previous approach because it eliminates the need for an explicit length calculation and reduces the number of conditional statements.

C++




#include <bits/stdc++.h>
using namespace std;
 
string conVowUpp(string str) {
    for (char& c : str) {
        if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
            c -= 32; // Subtracting 32 converts lowercase to uppercase in ASCII
        }
    }
    return str;
}
 
int main() {
    string str = "eutopia";
    cout << conVowUpp(str);
    return 0;
}


Java




public class GFG {
     
    // Function to convert vowels to uppercase in a string
    public static String conVowUpp(String str) {
        // Convert the string to a character array
        char[] charArray = str.toCharArray();
         
        // Iterate over each character in the array
        for (int i = 0; i < charArray.length; i++) {
            // Check if the character is a lowercase vowel (a, e, i, o, u)
            if (charArray[i] == 'a' || charArray[i] == 'e' || charArray[i] == 'i' || charArray[i] == 'o' || charArray[i] == 'u') {
                // Convert the lowercase vowel to uppercase by subtracting 32
                charArray[i] -= 32; // ASCII value of 'a' - ASCII value of 'A' = 97 - 65 = 32
            }
        }
         
        // Convert the character array back to a string
        String result = new String(charArray);
        return result;
    }
     
    public static void main(String[] args) {
        String str = "eutopia";
        System.out.println(conVowUpp(str));
    }
}


Python




def convert_vowels_to_uppercase(string):
    # Iterate through each character in the input string
    for i in range(len(string)):
        # Check if the character is a lowercase vowel
        if string[i] in ['a', 'e', 'i', 'o', 'u']:
            # Convert the lowercase vowel to uppercase in ASCII by subtracting 32
            string = string[:i] + chr(ord(string[i]) - 32) + string[i + 1:]
 
    return string
 
 
# Main function
if __name__ == "__main__":
    input_string = "eutopia"
    # Call the function to convert vowels to uppercase
    result_string = convert_vowels_to_uppercase(input_string)
    print(result_string)


C#




using System;
 
class Program
{
    static string ConvertVowelsToUppercase(string str)
    {
        // Convert lowercase vowels (a, e, i, o, u) to uppercase vowels (A, E, I, O, U)
        // in the given string.
        for (int i = 0; i < str.Length; i++)
        {
            char c = str[i];
            if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
            {
                // Subtracting 32 converts lowercase ASCII character to uppercase ASCII character.
                str = str.Remove(i, 1).Insert(i, ((char)(c - 32)).ToString());
            }
        }
        return str;
    }
 
    static void Main()
    {
        string str = "eutopia";
        string result = ConvertVowelsToUppercase(str);
        Console.WriteLine(result); // Output: "eUtOpIA"
    }
}


Javascript




// Function to convert vowels to uppercase in a given string
function conVowUpp(str) {
    let result = "";
 
    // Iterate through each character in the input string
    for (let i = 0; i < str.length; i++) {
        let c = str[i];
 
        // Check if the current character is a vowel (a, e, i, o, u)
        if (c === 'a' || c === 'e' || c === 'i' || c === 'o' || c === 'u') {
            // If it's a vowel, convert it to uppercase and add it to the result
            result += c.toUpperCase();
        } else {
            // If it's not a vowel, add it to the result as it is
            result += c;
        }
    }
 
    // Return the resulting string with vowels converted to uppercase
    return result;
}
 
// Test the conVowUpp function
let str = "eutopia";
console.log("Input string: " + str);
let modifiedStr = conVowUpp(str);
console.log("Modified string: " + modifiedStr);


Output:

EUtOpIA

Time Complexity: O(N)
Auxiliary Space: O(1)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads