C++ Program to display number of vowels, consonants, digits and white spaces in a string

Thursday, 22 December 2016

Statement :
Program to display number of vowels, consonants, digits and white spaces in a string.
Code : 
// Program to display number of vowels, consonants, digits and white spaces in a string.
#include<iostream>
#include<string>
using namespace std;
void main()
{
           cout << "\tDisplay Number Of Vowels, Consonants, Digits And Spaces In A String\n\n";
              string str;
              int v=0, c=0, d=0, s=0;
cout << "Enter A Line Of String : ";
              getline(cin, str);
              for (int i = 0; i!='\0'; i++)
              {
                     if (str[i] == 'A' || str[i] == 'a' || str[i] == 'E' || str[i] == 'e' || str[i] == 'I' || str[i] == 'i' || str[i] == 'O' || str[i] == 'o' || str[i] == 'U' || str[i] == 'u')
                     {
                           v++;
                     }
                     else if ((str[i] >= 'A' && str[i] <= 'Z') || (str[i] >= 'a' && str[i] <= 'z'))
                     {
                           c++;
                     }
                     else if (str[i] == ' ')
                     {
                           s++;
                     }
                     else if (str[i] >= '0' && str[i] <= '9')
                     {
                           d++;
                     }
                     else
                     {
                           //other cahracters
                     }
              }
              cout << "Number Of Spaces : " << s << endl;
              cout << "Number Of Vowels : " << v << endl;
              cout << "Number Of Consonant : " << c << endl;
              cout << "Number Of Digits : " << d << endl;
       cout << "\n\n";
       system("pause");

}




Mohammad Tayyab

Author & Editor

Imagination is more important than knowledge.

0 comments:

Post a Comment