Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i came through a question which states that to define a function vowelEncryption which accepts an argument str representing a string value which contains only alphabets.


.

 the function must encrypt the given string by replacing the consecutive vowels(one or more ) with their count then the function must return the encrypted string.

.
 for example : if the input is "eager" the output should be "2g1r". as we should replace the consecutive vowels with their count the string becomes 2g1r..
.
 example2: input :'kgooooooooooseee' output:'kg10s3'
'
 example3: input: 'ZAbcdeioufghiOAabcd' output: 'Z1bcd4fgh4bcd'


What I have tried:

s=input().strip()
vowels='aeiou'
ctr=0
count=0
for i in s:
    if i not in vowels:
        print(i,end="")
    else:
        count+=1
    print(count,end="")
Posted
Updated 15-Aug-21 1:27am

1 solution

You are only capturing single vowels. You need to use a flag to identify when the substring changes from vowel to consonant and vice versa. Something like:
Python
count=0
vowel = False
for letter in sentence:
    if letter in vowels:
        if vowel == False:
            vowel = True
            count = 1
        else:
            count += 1
    else:
        if vowel == True:
            print(count, end="")
            vowel = False
        print(letter, end='')
print('')


Note: I have not run exhaustive tests on this so you need to try with a number of different words.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900