Accessing characters in a string - C++ STL

C++ examples for STL:string

Description

Accessing characters in a string

Demo Code

#include <iostream>
#include <string>
#include <locale>

using std::string;

int main() {/*from w w  w  .j a va  2  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 (isalpha(text[i]))                    // Check for a letter
      switch (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;
}

Result


Related Tutorials