Skip to content

Instantly share code, notes, and snippets.

@Ch-sriram
Created July 10, 2020 12:42
Show Gist options
  • Save Ch-sriram/e9e17cea3eae611cbf4ab1d211842596 to your computer and use it in GitHub Desktop.
Save Ch-sriram/e9e17cea3eae611cbf4ab1d211842596 to your computer and use it in GitHub Desktop.
Find the number of words, vowels & consonants in the given string
// 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;
}
@Ch-sriram
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment