Problem: Write a Python program to count the number of vowels and consonants in a given string. Example: To count the number of vowels and consonants in a string, we iterate using a for loop through each character of the string and check if it matches a vowel. If yes
then, we increment the vowel counter otherwise increment the consonant counter. Output: Enter a string: Pencil Programmer Enter a string: Python In the above program, we have defined all vowels of the English alphabet in a list and use the same to check for vowels in the string. Inside the
If yes then it is a vowel, so we increment the vowel counter ( Finally, we output both counter’s values at the end of the program. In this program, our task is to count the total number of vowels and consonants present in the given string. As we know that, The characters a, e, i, o, u are known as vowels in the English alphabet. Any character other than that is known as the consonant. To solve this problem, First of all, we need to convert every upper-case character in the string to lower-case so that the comparisons can be done with the lower-case vowels only not upper-case vowels, i.e.(A, E, I, O, U). Then, we have to traverse the string using a for or while loop and match each character with all the vowels, i.e., a, e, I, o, u. If the match is found, increase the value of count by 1 otherwise continue with the normal flow of the program. The algorithm of the program is given below. Algorithm
ComplexityO(n) SolutionPythonOutput: Number of vowels: 10 Number of consonants: 17 COutput: Number of vowels: 10 Number of consonants: 17 JAVAOutput: Number of vowels: 10 Number of consonants: 17 C#Output: Number of vowels: 10 Number of consonants: 17 PHPOutput: Number of vowels: 10 Number of consonants: 17 C#, Java, Python, C++ Programaming Examples In this article, we will discuss the concept of the Python program to count vowels or consonants of the given string In this post, we are going to learn how to count the vowels and consonants in the given string in Python programming language The program allows the user to enter a string thereafter It counts the vowels and consonants of the given string using for loop in Python language Program 1: Write a program to count the number of vowels and consonants in a given string
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #Python program to count vowel or consonant of the given string defletters(input): return''.join([cforcininput
ifc.isalpha()]) str=input("Please enter a string as you wish: "); str=letters(str) vowels=0 consonants=0
foriinstr: if(i=='a'ori=='e'ori=='i'ori
=='o'ori=='u'or i=='A'ori=='E'ori==
'I'ori=='O'ori=='U'): vowels=vowels+1;#vowel counter is incremented by 1 else:
consonants=consonants+1; #consonant counter is incremented by 1 print("The number of vowels:",vowels); print("\nThe number of consonant:",consonants); When the above program is executed, it produces the following result
Approach
Python code to count the vowels and consonants using for loop – strlen() functionThe program allows the user to enter a string thereafter It counts the vowels and consonants of the given string using strlen() Program 2: Write a program to count the number of vowels and consonants in a given string
When the above program is executed, it produces the following result
Approach
You may also likeHow does python count vowels and consonants?Python. vcount = 0;. ccount = 0;. str = "This is a really simple sentence";. #Converting entire string to lower case to reduce the comparisons.. str = str.lower();. for i in range(0,len(str)):. #Checks whether a character is a vowel.. if str[i] in ('a',"e","i","o","u"):. How do you count consonants in a word in python?To count the number of vowels and consonants in a string, we iterate using a for loop through each character of the string and check if it matches a vowel. If yes then, we increment the vowel counter otherwise increment the consonant counter.
How do you count vowels and consonants in a string?Take the string as input. Take each character from this string to check. If this character is a vowel, increment the count of vowels. Else increment the count of consonants.
How do you count the number of vowels in python?Step 1: Take a string from the user and store it in a variable. Step 2: Initialize a count variable to 0. Step 3: Use a for loop to traverse through the characters in the string. Step 4: Use an if statement to check if the character is a vowel or not and increment the count variable if it is a vowel.
|