Given a string S, write a function replaceV that accepts a string and replace the occurrences of 3 consecutive vowels with _ in that string. Make sure to return and print the answer string.
Input:
aaahellooo
Output:
_hell_
Explanation:
Since aaa and ooo are consecutive 3 vowels and therefor replaced by _ .
Python Code:
def replaceV(S):
vowels=list("aeiouAEIOU")
L=list(S)
for i in range(len(L)-2):
if L[i] in vowels and L[i+1] in vowels and L[i+2] in vowels:
L[i]='*'
L[1+i]="*"
L[i+2]="*"
i+=2
return("".join(L).replace("***","_"))
print(replaceV(input()),end="")
Program Explanation:
This program takes an input string S and replaces any occurrence of three consecutive vowels with an underscore character "_" and then returns the modified string. Here's a breakdown of how the program works:
- The
replaceVfunction takes one argumentS, which is a string. - It defines a list
vowelscontaining all the vowels in lowercase and uppercase. - It converts the input string
Sinto a list of charactersLusing thelist()function. - It iterates over the characters in the list
Lusing a for loop that goes from 0 to the length of the list minus 2. - For each character
L[i],L[i+1], andL[i+2], it checks if they are all vowels using theinkeyword and thevowelslist. - If all three characters are vowels, it replaces them with an asterisk character "*" in the list
L. It also increments the indexiby 2 to skip over the next two characters since they have already been replaced. - After the loop is finished, it converts the modified list
Lback into a string using the"".join()method and replaces any occurrence of three asterisk characters "***" with an underscore character "_" using thereplace()method. - Finally, it prints the modified string using the
print()function with theendargument set to an empty string to prevent a newline character from being printed.