Finding the Number of Vowels, Consonants, Digits, and White Spaces in a String in C++
Hey programmers, in this tutorial we will learn how to write a code that gives the number of vowels, consonants, digits, and white spaces present in a string as its output.
Basic Terminologies:
- Vowels: The alphabets a, e, i, o, u are known as vowels.
- Consonants: The alphabets which are not vowels are known as consonants.
- Digits: The numbers 0 to 9 are known as digits.
- WhiteSpaces: A space is known as white space.
we write the program by using conditional statements such as if and if..else.
IMPLEMENTATION:
STEP 1: We include the required headerfiles and namespaces into the program. We use the headerfile iostream as we are dealing with cout, cin in our program, and we also use the headerfile cstring since we are using a string function strlen() in our program which is present in c language. As we are using the headerfile of c-language we need to specify it by preceding the headerfile name with a letter ‘c’.
Hence, we include the headerfiles and namespaces into the program as follows:
#include <iostream> #include <cstring> using namespace std;
STEP 2: We start writing the main() function in which we use conditional statements to count the number of vowels, consonants, digits, and white spaces.
DECLARATIONS:
- We need to declare a character array and assign a string to it.
- We declare integer type of variables such as ‘i’ to iterate over the string using for loop.
- vowelCount variable of integer type to count the number of vowels present in the string.
- consonantCount variable of integer type to count the number of consonants present in the string.
- digitCount variable of integer type to count the number of digits present in the string.
- whiteSpaceCount variable of integer type to count the number of spaces encountered over the string.
Initially, we need to assign the variables vowelCount, consonantCount, digitCount, and whiteSpaceCount with the value ‘0’.
char c[65] = "I have learnt 2 programming languages from CodeSpeedy"; int i, vowelCount=0, consonantCount=0, digitCount=0, whiteSpaceCount=0;
STEP 3: We use a for loop which iterates over the string character by character.
While iterating over the string, if it encounters a character to be a space, digit, vowel, consonant then we have to increment the value of whiteSpaceCount, digitCount, vowelCount, consonantCount 1 respectively.
for(i=0;i<strlen(c);i++) { if(c[i]==' ') { whiteSpaceCount++; } if(c[i]>='0' && c[i]<='9') { digitCount++; } if(c[i]=='a' || c[i]=='A' || c[i]=='e' ||c[i]=='E' ||c[i]=='i' ||c[i]=='I' ||c[i]=='o' ||c[i]=='O' ||c[i]=='u' ||c[i]=='U') { vowelCount++; } else if((c[i]>='a' && c[i]<='z') || (c[i]>='A' && c[i]<='Z')) { consonantCount++; } }
STEP 4: Lastly, display the number of vowels, consonants, digits, and white spaces encountered over the string to the user.
cout<<"Vowel Count = "<<vowelCount<<"\n"; cout<<"Consonant Count = "<<consonantCount<<"\n"; cout<<"Digit Count = "<<digitCount<<"\n"; cout<<"White Space Count = "<<whiteSpaceCount<<"\n";
CODE:
The whole code is as follows:
#include <iostream> #include <cstring> using namespace std; int main() { char c[65] = "I have learnt 2 programming languages from CodeSpeedy"; int i, vowelCount=0, consonantCount=0, digitCount=0, whiteSpaceCount=0; for(i=0;i<strlen(c);i++) { if(c[i]==' ') { whiteSpaceCount++; } if(c[i]>='0' && c[i]<='9') { digitCount++; } if(c[i]=='a' || c[i]=='A' || c[i]=='e' ||c[i]=='E' ||c[i]=='i' ||c[i]=='I' ||c[i]=='o' ||c[i]=='O' ||c[i]=='u' ||c[i]=='U') { vowelCount++; } else if((c[i]>='a' && c[i]<='z') || (c[i]>='A' && c[i]<='Z')) { consonantCount++; } } cout<<"Vowel Count = "<<vowelCount<<"\n"; cout<<"Consonant Count = "<<consonantCount<<"\n"; cout<<"Digit Count = "<<digitCount<<"\n"; cout<<"White Space Count = "<<whiteSpaceCount<<"\n"; }
OUTPUT:
Vowel Count = 17 Consonant Count = 28 Digit Count = 1 White Space Count = 7
Leave a Reply