switch statement based on char value : string subscript indexer « string « C++ Tutorial






#include <iostream>
#include <string>
#include <cctype>
using std::cout;
using std::cin;
using std::endl;
using std::string;

int main() {
  string text = "asdffdsaasdf";


  int vowels = 0;              
  int consonants = 0;          
  for(int i = 0 ; i < text.length() ; i++)
    if(std::isalpha(text[i]))  
      switch(std::tolower(text[i])) {
        case 'a': case 'e': case 'i':
        case 'o': case 'u':
          vowels++;
          break;
        default:
          consonants++;
      }

  cout << "Your input contained "
       << vowels     << " vowels and "
       << consonants << " consonants."
       << endl;

  return 0;
}
Your input contained 3 vowels and 9 consonants.








15.5.string subscript indexer
15.5.1.Modify char in a string by indexer
15.5.2.Accessing characters in a string
15.5.3.switch statement based on char value
15.5.4.accessing characters in string objects
15.5.5.Is it a string::npos