C++ string at() Access a char from string one by one

Description

C++ string at() Access a char from string one by one

#include <iostream>
#include <string>
using namespace std;
int main()//from w  w w . jav  a 2  s.c  om
{
   string str = "this is a test and a letter vowels radio";
   int i, numChars;
   int vowelCount = 0;
   cout << "The string: " << str << endl;
   numChars = int(str.length());
   for (i = 0; i < numChars; i++)
   {
      switch (str.at(i))  // here's where a character is retrieved
      {
         case 'a':
         case 'e':
         case 'i':
         case 'o':
         case 'u':
         vowelCount++;
      }
   }
   cout << "has " << vowelCount << " vowels." << endl;
   return 0;
}



PreviousNext

Related