Introducing Radical.sh

Forget Code launches a powerful code generator for building API's

Accessing characters in a string in C++

#include <iostream>
#include <string>
#include <cctype>
using std::cout;
using std::cin;
using std::endl;
using std::string;
 
int main() {
  string text = "Forgetcode";
  char c;
  int numofvowels = 0;              
  int numofconsonants = 0;  
  int k=text.length();
  for(int i = 0 ; i < k ; i++)
    if(isalpha(text[i]))
         c=tolower(text[i]);
      switch(c) 
       {                   
        case 'a':
        case 'e':
        case 'i':
        case 'o': 
        case 'u':
        numofvowels++;
        break;
        default:
        numofconsonants++;
      }
   cout << "Given string: "<<text;
   cout<<"\nNumber of vowels:"<<numofvowels;
   cout <<"\nNumber of consonants:" << numofconsonants<<endl;
  return 0;
}