Tuesday 24 June 2014

C++ program to count number of characters, vowels, consonants and white space in a string


Here is a C++ program to count number of characters, vowels, consonants and white space in a string


 SOURCE CODE OUTPUT
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
    char str[100],temp;
    int nvowel=0,ncont=0,nspace=0;
    cout<<"Enter string : ";
    cin.getline(str,100);
    for(int i=0;str[i]!='\0';i++)
    {
        if(str[i]=='a'||str[i]=='e'||str[i]=='i'||str[i]=='o'||str[i]=='u')
            nvowel++;
        else if(str[i]=='A'||str[i]=='E'||str[i]=='I'||str[i]=='O'||str[i]=='U')
            nvowel++;
        else if(str[i]==' ')
             nspace++;
        else
            ncont++;
    }
    cout<<"Number of vowels     : "<<nvowel<<endl;
    cout<<"Number of consonants : "<<ncont<<endl;
    cout<<"Number of spaces     : "<<nspace<<endl;
    return 0;
}

Any questions regarding to program please write in comments.

No comments:

Post a Comment