Showing posts with label 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.. Show all posts
Showing posts with label 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.. Show all posts
Saturday, March 11, 2023

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.



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:

  1. The replaceV function takes one argument S, which is a string.
  2. It defines a list vowels containing all the vowels in lowercase and uppercase.
  3. It converts the input string S into a list of characters L using the list() function.
  4. It iterates over the characters in the list L using a for loop that goes from 0 to the length of the list minus 2.
  5. For each character L[i], L[i+1], and L[i+2], it checks if they are all vowels using the in keyword and the vowels list.
  6. If all three characters are vowels, it replaces them with an asterisk character "*" in the list L. It also increments the index i by 2 to skip over the next two characters since they have already been replaced.
  7. After the loop is finished, it converts the modified list L back into a string using the "".join() method and replaces any occurrence of three asterisk characters "***" with an underscore character "_" using the replace() method.
  8. Finally, it prints the modified string using the print() function with the end argument set to an empty string to prevent a newline character from being printed.