eli2010
eli2010

Reputation: 1

How to remove vowels from a string, and store result in another string

I want to program a console application to remove vowels from a string, storing the result (string without vowels) in another string.

How should I do it??

#include <iostream>

using namespace std;
int main(){
string a,b;
int s=0,r,t;
cin>>a;
int k=a.size();
for(int y=0;y<k;y++){
        if(a[y]==65||a[y]==97||a[y]==69||a[y]==101||a[y]==73||a[y]==105||a[y]==79||a[y]==111||a[y]==85||a[y]==117||a[y]==89||a[y]==121){
        }
        else{
            b[s]=a[y];



            s++;

        }



}
    cout<<b;


    return 0;
}

Upvotes: 0

Views: 275

Answers (1)

User
User

Reputation: 610

There are couple of things I want to highlight

Try to avoid comparisons with ASCII a[y]==97 instead you can use a[y]=='a'

Always include header #include < string > while dealing with string functions and output

No need to do like this it's hard to read and understand you can create an array/Vector of characters like

std::vector<char> vowels{'A','E','I','O','U','a','e','i','o','u'}

Use getline() instead of cin \\ You can visit getline

You can do a case insensitive comparison if it's the need

This is my version of code

int main(){
string a,b;
std::vector<char> vowels{'A','E','I','O','U','a','e','i','o','u'};
getline(cin,a);
for(auto ch:a){
        if(find(vowels.begin(),vowels.end(),ch)!=vowels.end()){
            continue;
        }
        else{
            b+=a
        }
}
    cout<<b;


    return 0;
}

Upvotes: 1

Related Questions