All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Count Vowels In A String Python Using While Loop

Last Updated : Jul 1, 2023

Count Vowels In A String Python Using While Loop

In this article we will show you the solution of count vowels in a string python using while loop, this program's objective is to determine how many vowels and consonants are there in a given string.

The English alphabet's vowel letters were the letter a, e, i, o, & u. As opposed to that, consonants are other characters.

For solving the problem is to convert all upper-case characters in the string to lower-case so that we can do comparisons using only lower-case vowels, i.e. (A, E, I, O, U) instead of upper-case vowels.

We will now discuss the idea of count vowels in a string python using while loop with example.

Step By Step Guide On Count Vowels In A String Python Using While Loop :-

public class countvowelconsonant {
    public static void main(String[] args) {
        //Counter variable to store the count of vowels and consonant
        int vCount = 0, cCount = 0;
        //Declare a string
        String str = "This is a really simple sentence";
        //Converting entire string to lower case to reduce the comparisons
        str = str.toLowerCase();
        for(int i = 0; i < str.length(); i++) {
            //Checks whether a character is a vowel
            if(str.charAt(i) == 'a' || str.charAt(i) == 'e' || str.charAt(i) == 'i' || str.charAt(i) == 'o' || str.charAt(i) == 'u') {
                //Increments the vowel counter
                vCount++;
            }
            //Checks whether a character is a consonant
            else if(str.charAt(i) >= 'a' && str.charAt(i)<='z') {
                //Increments the consonant counter
                cCount++;
            }
        }
        System.out.println("Number of vowels: " + vCount);
        System.out.println("Number of consonants: " + cCount);
    }
}
  1. The first step is to create a class called "countvowelconsonant".
  2. A main method within the class should contain the code to count how many vowels and consonants there are in the given string.
  3. Keep track of vowels and consonants by initializing two variables, "vCount" and "cCount".
  4. The first step is to declare a string variable called "str" and assign its value to the string you want to count the vowels and consonants in.
  5. To change the full string to lowercase, use the toLowerCase() function. The only comparison needed is for lowercase vowels and consonants to reduce the number of comparisons.
  6. Iterate through all the characters in the string using a for loop.
  7. Within the for loop, an if statement can be used to determine whether the present character is a vowel. To determine if the character is 'a', 'e', 'i', 'o', or 'u', we need to check for the character type. The vCount variable should be incremented if it is.
  8. You can use another if statement to determine whether the current character is a vowel or a consonant. To do this, determine whether the character was a lower-case letter between the letters a and z. The cCount variable should be incremented if that is the case.
  9. After the for loop has finished iterating through each character of the string, print out the value of vCount and cCount using the System.out.println() method.

Conclusion :-

As a result, we have successfully learned how to count vowels in a string python using while loop.

The use of a foreach loop to go through the string and match every character with each of the vowels a, e, i, o, or u one by one.

In the absence of a match, you should carry on with the program's regular flow while increasing the value to count by 1.

I hope this article on count vowels in a string python using while loop helps you and the steps and method mentioned above are easy to follow and implement.