Friday, December 27, 2024
Google search engine
HomeLanguagesPython – Replace Consonants by i, Vowels by j

Python – Replace Consonants by i, Vowels by j

Given a String, perform the replacement of all the vowels with i, and all the consonants using j.

Input : test_str = ‘neveropen’, i, j = “A”, “B” 
Output : BAABBBABBAABB 
Explanation : All vowels replaced by A and consonants by B.

Input : test_str = ‘gfg’, i, j = “A”, “B” 
Output : BBB 
Explanation : Only consonants present and replaced by B.

Method #1 : Using sub() + regex

In this, we use sub-function and pass regex for consonants and vowel to perform an appropriate replacement. 

Python3




# Python3 code to demonstrate working of
# Replace Consonants by i, Vowels by j
# Using  sub() + regex
import re
 
# initializing strings
test_str = 'neveropen'
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing i, j
i, j = "V", "C"
 
# the negation of vowel regex is a consonant, denoted by "^"
res = re.sub("[^aeiouAEIOU]", j, test_str)
res = re.sub("[aeiouAEIOU]", i, res)
 
# printing result
print("The string after required replacement : " + str(res))


Output

The original string is :neveropen
The string after required replacement : CVVCCCVCCVVCC

Time complexity: O(n), where n is the length of the input string “test_str”.
Auxiliary space: O(1). It uses only a few extra variables (i, j, res) to store intermediate results. 

Method #2 : Using maketrans() + symmetric difference

In this, we first get consonants using symmetric difference of vowels and maketrans is function that is used to perform the task of replacement of Strings.

Python3




# Python3 code to demonstrate working of
# Replace Consonants by i, Vowels by j
# Using maketrans() + symmetric difference
import string
 
# initializing strings
test_str = 'neveropen'
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing i, j
i, j = "V", "C"
 
# extracting vowels and consonants
Vows = 'aeiouAEIOU'
 
# using sym. diff to get consonants
Cons = ''.join(set(string.ascii_letters).difference(set(Vows)))
 
# initializing translation
translation = str.maketrans(Vows + Cons, i * len(Vows) + j * len(Cons))
 
res = test_str.translate(translation)
 
# printing result
print("The string after required replacement : " + str(res))


Output

The original string is :neveropen
The string after required replacement : CVVCCCVCCVVCC

Time Complexity: O(n), where n is the length of the input string.
Auxiliary Space: O(1), since no extra data structure is used.

Method #3: Using replace()

Initialize a string with vowels.Iterate a for loop on the given string and use in operator to check whether character is in vowel string or not.If True then use replace() method to replace the character by j, if False replace the character by i.

Python3




# Python3 code to demonstrate working of
# Replace Consonants by i, Vowels by j
 
# initializing strings
test_str = 'neveropen'
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing i, j
i, j = "V", "C"
vow="aeiouAEIOU"
for k in test_str:
    if k in vow:
        test_str=test_str.replace(k,i)
    else:
        test_str=test_str.replace(k,j)
 
res=test_str
# printing result
print("The string after required replacement : " + str(res))


Output

The original string is :neveropen
The string after required replacement : CVVCCCVCCVVCC

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #4 : Using for loop

Python3




# Python3 code to demonstrate working of
# Replace Consonants by i, Vowels by j
 
# initializing strings
test_str = 'neveropen'
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing i, j
i, j = "V", "C"
vow="aeiouAEIOU"
res=""
for k in test_str:
    if k in vow:
        res+=i
    else:
        res+=j
 
 
# printing result
print("The string after required replacement : " + str(res))


Output

The original string is :neveropen
The string after required replacement : CVVCCCVCCVVCC

Time complexity: O(n) where n is the length of the input string ‘test_str’.
Auxiliary space: O(n) where n is the length of the input string ‘test_str’, as we are creating a new string ‘res’ to store the result.

Method#5: Using list comprehension.

You can use a list comprehension to loop through the string and check for each character if it’s a vowel or consonant. If it’s a vowel, append “V” to a new list and if it’s a consonant append “C” to the new list. Finally, join the list to get the modified string.

Python3




# Initializing the string
test_str = 'neveropen'
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing i, j
i,j='V','C'
 
# Using list comprehension to replace vowels and consonants
res = "".join([i if c in "aeiouAEIOU" else j for c in test_str])
 
# printing result
print("The string after required replacement : " + str(res))


Output

The original string is :neveropen
The string after required replacement : CVVCCCVCCVVCC

Time complexity: O(n), where n is the length of the input string ‘test_str’.

Auxiliary space: O(n), as a new string ‘res’ is created of length n. 

Method #6 : Using ord() and replace() methods

Python3




# Python3 code to demonstrate working of
# Replace Consonants by i, Vowels by j
 
# initializing strings
test_str = 'neveropen'
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing i, j
i, j = "V", "C"
vow=[97, 101, 105, 111, 117, 65, 69, 73, 79, 85]
for k in test_str:
    if ord(k) in vow:
        test_str=test_str.replace(k,i)
    else:
        test_str=test_str.replace(k,j)
 
res=test_str
# printing result
print("The string after required replacement : " + str(res))


Output

The original string is :neveropen
The string after required replacement : CVVCCCVCCVVCC

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #7 : Using operator.countOf() method

Python3




# Python3 code to demonstrate working of
# Replace Consonants by i, Vowels by j
import operator as op
# initializing strings
test_str = 'neveropen'
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing i, j
i, j = "V", "C"
vowels="aeiouAEIOU"
res=""
for k in test_str:
    if op.countOf(vowels,k)>0:
        res+=i
    else:
        res+=j
 
 
# printing result
print("The string after required replacement : " + str(res))


Output

The original string is :neveropen
The string after required replacement : CVVCCCVCCVVCC

Time Complexity: O(n)
Auxiliary Space: O(n)

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments