C++ string Accessing characters in a string
#include <iostream> #include <string> #include <locale> using std::string; int main() {/* www. ja va2 s .c o m*/ string text; // Stores the input std::cout << "Enter a line of text:\n"; std::getline(std::cin, text); // Read a line including spaces int vowels {}; // Count of vowels int consonants {}; // Count of consonants for(int i {} ; i<text.length() ; ++i) { if(std::isalpha(text[i])) // Check for a letter switch(std::tolower(text[i])) // Convert to lowercase { case 'a': case 'e': case 'i': case 'o': case 'u': ++vowels; break; default: ++consonants; break; } } std::cout << "Your input contained " << vowels << " vowels and " << consonants << " consonants." << std::endl; }