Advertisement
singam_dev57

Algorithm to Count Vowels and Consonants in a String

Dec 27th, 2024 (edited)
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.56 KB | Source Code | 0 0
  1. def count_vowels_consonants():
  2.     text = "How many vowels and consonants are in this sentence?"
  3.     vowels_count = 0
  4.     consonants_count = 0
  5.  
  6.     for char in text:
  7.         if char in ("aeiou"):
  8.             vowels_count = vowels_count + 1
  9.         elif char.isalpha():
  10.             consonants_count = consonants_count + 1
  11.  
  12.     return vowels_count, consonants_count
  13.  
  14. vowels_count, consonants_count = count_vowels_consonants()
  15.  
  16. print(f"The number of vowels in the string is: {vowels_count}")
  17. print(f"The number of consonants in the string is: {consonants_count}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement