Sorting Vowels, Consonants, Digits and Other Characters in a String in C++

//Sorting Vowels, Consonants, Digits and Other Characters in a String in C++ by soulrazzmatazz13@facebook.com
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <string>


using namespace std;

int main()
{
    int vow,con,d,s;
    vow=con=d=s=0;

    string str;
    cout<<"Enter your statement: "<<endl;
    getline(cin,str);


    for (int i=0; i < str.length()  ; i++)
    {

        if(str.at(i)=='a'||str.at(i)=='e'||str.at(i)=='i'||str.at(i)=='o'||str.at(i)=='u'||str.at(i)=='A'||str.at(i)=='E'||str.at(i)=='I'||str.at(i)=='O'||str.at(i)=='U')
            vow++;
        else if((str.at(i)>='a'&&str.at(i)<='z')||(str.at(i)>='A'&&str.at(i)<='Z'))
            con++;
        else if(str.at(i)>='0'&&str.at(i)<='9')
            d++;
        else if(str.at(i)!=' '||str.at(i)!=vow||str.at(i)!=con||str.at(i)!=d)
            s++;\

    }
        cout<<"Vowels: "<<vow<<'\n';
        cout<<"Consonants: "<<con<<'\n';
        cout<<"Digits: "<<d<<'\n';
        cout<<"Other Characters: "<<s<<'\n';



    return 0;
}
Last edited on
And?
What is your question?
BTW: use code tag. It's "<>" button on the right.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//Other alternatives.

#include <iostream>
#include <string>

using namespace std;
int main(){
    string sentence;    //variable to hold the sentence
    char ch;            //variable to hold each character
    string vow ("aeiou");
    string con ("bcdfghjklmnpqrstvwqyz");
    string dig ("0123456789");

    int found;
    int ctr_vow=0;
    int ctr_con=0;
    int ctr_dig=0;
    int str_spe=0;

    cout << "Enter a sentence:";
    getline(cin, sentence);     //get input from keyboard

    //loop through every character in the string
    for (int i=0; i < sentence.length(); i++){
        ch=  sentence.at(i);    //assign current char to ch

        found=vow.find(tolower(ch));
        if (found!=string::npos)
            ctr_vow++;

        found=con.find(tolower(ch));
        if (found!=string::npos)
            ctr_con++;

    }

    cout << "Vowels: " << ctr_vow << endl;
    cout << "Consonants: " << ctr_con << endl;
    return 0;
}
Last edited on
Topic archived. No new replies allowed.