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
- Define a string.
- Convert the string to lower case so that comparisons can be reduced. Else we need to compare with capital (A, E, I, O, U).
- If any character in string matches with vowels (a, e, i, o, u ) then increment the vcount by 1.
- If any character lies between 'a' and 'z' except vowels, then increment the count for ccount by 1.
- Print both the counts.
Complexity
O(n)
Solution
Python
Output:
Number of vowels: 10 Number of consonants: 17
C
Output:
Number of vowels: 10 Number of consonants: 17
JAVA
Output:
Number of vowels: 10 Number of consonants: 17
C#
Output:
Number of vowels: 10 Number of consonants: 17
PHP
Output:
Number of vowels: 10 Number of consonants: 17
Here is one of the solutions to your problem. Keep vowels, consonants and all characters in separate sets, read one character at the time from a file. If match is found in any of the sets then increment the appropriate counter. Calculate the percentage based on those counters:
#include <iostream> #include <fstream> #include <set> #include <algorithm> using namespace std; int main() { set<char> vowels = { 'a', 'e', 'i', 'o', 'u' }; set<char> consonants = { 'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'x', 'z', 'w', 'y' }; set<char> allchars = { 'a', 'e', 'i', 'o', 'u', 'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'x', 'z', 'w', 'y' }; char ch; int vowelsCount = 0; int consonantsCount = 0; int percentageVowels = 0; int percentageConsonants = 0; int charactersCount = 0; fstream fin("MyFile.txt", fstream::in); while (fin >> ch) { ch = tolower(ch); if (find(allchars.begin(), allchars.end(), ch) != allchars.end()) { charactersCount++; } if (find(vowels.begin(), vowels.end(), ch) != vowels.end()) { vowelsCount++; } if (find(consonants.begin(), consonants.end(), ch) != consonants.end()) { consonantsCount++; } } percentageVowels = double(vowelsCount) / charactersCount * 100; percentageConsonants = double(consonantsCount) / charactersCount * 100; cout << "Vowels %: " << percentageVowels << endl; cout << "Consonants %: " << percentageConsonants << endl; getchar(); return 0; }
Jump to
- Exercise 8(A)
- Exercise 8(B)
- Exercise 8(C)
- Integers
- Rational Numbers
- Fractions (Including Problems)
- Decimal Fractions (Decimals)
- Exponents (including Laws of Exponents)
- Ratio and Proportion (Including Sharing in a Ratio)
- Unitary Method (including Time and Work)
- Percent and Percentage
- Profit, Loss and Discount
- Simple Interest
- Fundamental Concepts (Including Fundamental Operations)
- Simple Linear Equations (Including Word Problems)
- Set Concepts (Some Simple Divisions by Vedic Method)
- Lines and Angles (Including Construction of angles)
- Triangles
- Pythagoras Theorem
- Symmetry (Including Reflection and Rotation)
- Recognition of Solids (Representing 3-D in 2-D)
- Congruency: Congruent Triangles
- Mensuration
- Data Handling
- Probability
Selina Solutions Class 7 Mathematics Solutions for Exercise 8(B) in Chapter 8 - Percent and Percentage
Q6) What is the percentage of vowels in the English alphabet?
Answer:
Solution:
There are 5 vowels in 26 English alphabets
∴ Percentage of vowels = \frac{5\times100}{26}=\frac{250}{13}=19\frac{3}{13}\%
Video transcript
Hi, let's see what we have to do in this question so the question says the percentage of words are displayed in the end English [Music] right divided by total alphabets so if we fight by 26 into 100 which will give us 19 3 by Right, this is the answer thank you
Was This helpful?
Quick Links
Terms & Policies
2022 © Quality Tutorials Pvt Ltd All rights reserved