Algorithm To Count Vowels And Consonants In A String
ALGORITHM:
- Get an input string from the user.
- Initialize variables “i” to 0, “vowels” to 0, “consonants” to 0, and “length” to 0.
- Repeat the following until the end of the string is reached:
- a. Increment the value of “length” by 1 to count the current character.
- b. Increment the value of “i” by 1 to move to the next character.
4.Initialize a variable “j” to 0.
5.Repeat the following until “j” is less than “length”:
- a. If the current character at index “j” is a vowel (either lowercase or uppercase), increment the value of “vowels” by 1.
- b. If the current character at index “j” is a space, move on to the next character (by incrementing the value of “j” by 1).
- c. Otherwise, the current character is a consonant, so increment the value of “consonants” by 1 and move on to the next character (by incrementing the value of “j” by 1).
6.Print out the number of vowels and consonants in the string.
DETAILED EXPLANATION:
This algorithm counts the number of vowels and consonants in a given input string.
It first counts the length of the string, and then iterates through each character to check if it is a vowel or a consonant.
If it is a vowel, the “vowels” count is incremented; if it is a space, the algorithm skips that character; and if it is a consonant, the “consonants” count is incremented.
Finally, the counts of vowels and consonants are printed out.
FLOWCHART: