on Leave a Comment

C++ Program to Count Vowels Consonants Digits Spaces in String

C++ Program to Count Vowels Consonants Digits Spaces in String

TOPICS:

(1) C++ Program to Count Vowels Consonants Digits Spaces in C-Style String.

(2) C++ Program to Count Vowels Consonants Digits Spaces in String Object.

C++ Program to Count Vowels Consonants Digits Spaces in C-Style String

#include<iostream>
using namespace std;
int main()
{
    char str[100];
    int v, c, d, s;
    v =  c = d = s = 0;
    cout << "Enter a line of string: ";
    cin.getline(str, 150);
    for(int i = 0; str[i]!='\0'; ++i)
    {
        if(str[i]=='a' || str[i]=='e' || str[i]=='i' || str[i]=='o' || str[i]=='u' || str[i]=='A' || str[i]=='E' || str[i]=='I' || str[i]=='O' || str[i]=='U')
        {
            ++v;
        }
        else if((str[i]>='a'&& str[i]<='z') || (str[i]>='A'&& str[i]<='Z'))
        {
            ++c;
        }
        else if(str[i]>='0' && str[i]<='9')
        {
            ++d;
        }
        else if (str[i]==' ')
        {
            ++s;
        }
    }
    cout << "Vowels: " << v;
    cout << "\nConsonants: " << c;
    cout << "\nDigits: " << d;
    cout << "\nWhite spaces: " << s;
    return 0;
}

OUTPUT:

Enter a line of string: Welcome to Pc Technical Pro
Vowels: 8
Consonants: 15
Digits: 0
White spaces: 4

C++ Program to Count Vowels Consonants Digits Spaces in String Object

#include<iostream>
using namespace std;
int main()
{
    string str;
    int v, c, d, s;
    v =  c = d = s = 0;
    cout << "Enter a line of string: ";
    getline(cin, str);
    for(int i = 0; i<str.length(); i++)
    {
         if(str[i]=='a' || str[i]=='e' || str[i]=='i' ||
           str[i]=='o' || str[i]=='u' || str[i]=='A' ||
           str[i]=='E' || str[i]=='I' || str[i]=='O' ||
           str[i]=='U')
        {
            ++v;
        }
        else if((str[i]>='A'&& str[i]<='Z') || (str[i]>='a'&& str[i]<='z'))
        {
            ++c;
        }
        else if(str[i]>='0' && str[i]<='9')
        {
            ++d;
        }
        else if (str[i]==' ')
        {
            ++s;
        }
    }
    cout << "Vowels: " << v;
    cout << "\nConsonants: " << c;
    cout << "\nDigits: " << d;
    cout << "\nWhite spaces: " << s;
    return 0;
}

OUTPUT:

Enter a line of string: Welcome to Pc Technical Pro
Vowels: 8
Consonants: 15
Digits: 0
White spaces: 4

See also:





0 comments:

Post a Comment