User Tools

Site Tools


c_-_c_strings:accessing_characters_in_a_string

C - C++ Strings - Accessing characters in a string

#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;
}
c_-_c_strings/accessing_characters_in_a_string.txt · Last modified: 2020/07/15 10:30 by 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki