Access a char from string one by one with at() function - C++ STL

C++ examples for STL:string

Description

Access a char from string one by one with at() function

Demo Code

#include <iostream>
#include <string>
using namespace std;
int main()/*  www . j  a va2  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;
}

Result


Related Tutorials