Convert vowels into upper case character in a given string
Last Updated :
03 Oct, 2023
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++
#include <bits/stdc++.h>
using namespace std;
string conVowUpp(string& 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;
}
int main()
{
string str = "eutopia" ;
cout << conVowUpp(str);
}
|
Java
import java.util.*;
class GFG{
static void conVowUpp( char [] 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);
}
public static void main(String[] args)
{
String str = "eutopia" ;
conVowUpp(str.toCharArray());
}
}
|
Python3
def conVowUpp( 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)
if __name__ = = '__main__' :
str = "eutopia"
conVowUpp( str )
|
C#
using System;
class GFG{
static void conVowUpp( char [] 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);
}
public static void Main(String[] args)
{
String str = "eutopia" ;
conVowUpp(str.ToCharArray());
}
}
|
Javascript
<script>
function conVowUpp(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]);
}
}
}
var str = "eutopia" ;
conVowUpp(str);
</script>
|
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;
}
}
return str;
}
int main() {
string str = "eutopia" ;
cout << conVowUpp(str);
return 0;
}
|
Java
public class GFG {
public static String conVowUpp(String str) {
char [] charArray = str.toCharArray();
for ( int i = 0 ; i < charArray.length; i++) {
if (charArray[i] == 'a' || charArray[i] == 'e' || charArray[i] == 'i' || charArray[i] == 'o' || charArray[i] == 'u' ) {
charArray[i] -= 32 ;
}
}
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):
for i in range ( len (string)):
if string[i] in [ 'a' , 'e' , 'i' , 'o' , 'u' ]:
string = string[:i] + chr ( ord (string[i]) - 32 ) + string[i + 1 :]
return string
if __name__ = = "__main__" :
input_string = "eutopia"
result_string = convert_vowels_to_uppercase(input_string)
print (result_string)
|
C#
using System;
class Program
{
static string ConvertVowelsToUppercase( string str)
{
for ( int i = 0; i < str.Length; i++)
{
char c = str[i];
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' )
{
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);
}
}
|
Javascript
function conVowUpp(str) {
let result = "" ;
for (let i = 0; i < str.length; i++) {
let c = str[i];
if (c === 'a' || c === 'e' || c === 'i' || c === 'o' || c === 'u' ) {
result += c.toUpperCase();
} else {
result += c;
}
}
return result;
}
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
Share your thoughts in the comments
Please Login to comment...