Created
July 10, 2020 12:42
-
-
Save Ch-sriram/e9e17cea3eae611cbf4ab1d211842596 to your computer and use it in GitHub Desktop.
Find the number of words, vowels & consonants in the given string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Problem link: https://www.geeksforgeeks.org/program-count-vowels-consonant-digits-special-characters-string/ | |
#include <vector> | |
#include <sstream> | |
#include <iostream> | |
using namespace std; | |
bool is_vowel(const char &ch) { | |
string vowels = "aeiouAEIOU"; | |
for(const auto &c: vowels) | |
if(c == ch) | |
return true; | |
return false; | |
} | |
void get_ans(string &sentence) { | |
getline(cin, sentence); | |
stringstream ss(sentence); | |
vector<string> tokens; | |
string str; | |
while(getline(ss, str, ' ')) | |
if(str != "" && str != " ") | |
tokens.push_back(str); | |
int vowels = 0, consonants = 0; | |
for(const string &s: tokens) | |
for(const char &c: s) | |
if(is_vowel(c)) ++vowels; | |
else ++consonants; | |
cout << tokens.size() << " " << vowels << " " << consonants; | |
} | |
int main() { | |
int t; cin >> t; | |
cin.ignore(); | |
while(t--) { | |
string sentence; | |
get_ans(sentence); | |
cout << endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Problem Link