Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
counting vowels in a string
#1
Hi,

I'm moving from a C background to python. I'm trying to count the number of vowels in a string. I've put the vowels in a list, and I'm trying to see if the list's 1st vowel is found at a string position. If true, it logs it. If false, the string position is increased. This process continues until the string position matches the length of the string.

After this, the next vowel in the list is used, and the process starts over again.

This continues until all vowels have been tried.

In running the code, it only returns 0, so I'm not sure where my logic is going wrong.

Note that I had thought about using the string method, rindex(value, start, end), and then increasing the start and end values by 1 each pass (where they begin at start = 0, end = 1), and using the value as a reference to the vowel list, but I couldn't exclude the error handling of when a vowel wasn't found (i.e. pass over it).

Thoughts?

txt = "Count the number of vowels in this sentence."

#format: txt.rindex(value, start, end)

txt = ["a","e","i","o","u"] #vowels to test for

element = 0
counter = 0
position = 0


while position < len(txt) and element < len(txt):
    
    if txt[element] == txt[position] and position < len(txt):
        print("vowel counter = ", counter)
        position += 1 # inc list index position by 1
        
    else:
        # go to next vowel to search for
        # reset position to 0 to next vowel can be search for
        element += 1
        position = 0
        
print("Search complete, total vowels found: ", counter)
Reply


Messages In This Thread
counting vowels in a string - by project_science - Dec-30-2020, 05:18 PM
RE: counting vowels in a string - by jefsummers - Dec-30-2020, 05:59 PM
RE: counting vowels in a string - by deanhystad - Dec-30-2020, 06:06 PM
RE: counting vowels in a string - by buran - Dec-30-2020, 06:44 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  remove vowels in word with conditional ambrozote 12 6,510 May-02-2021, 06:57 PM
Last Post: perfringo
  Counting vowels in a string (PyBite #106) Drone4four 4 3,332 Jul-07-2020, 05:29 AM
Last Post: theknowshares
  Counting the number of letters in a string alphabetically TreasureDragon 2 3,848 Mar-07-2019, 01:03 AM
Last Post: TreasureDragon
  Counting number of characters in a string Drone4four 1 4,377 Aug-16-2018, 02:33 PM
Last Post: ichabod801
  no vowels function alex_bgtv 6 6,219 Jan-01-2018, 08:48 PM
Last Post: alex_bgtv
  replace vowels niru 9 23,427 Sep-26-2017, 12:46 PM
Last Post: nilamo
  Vowels and Consonants detector OmarSinno 5 11,205 Sep-21-2017, 02:27 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020