need help in how to remove vowel from the string?

Aug 3, 2012 at 6:10am
this is the program to remove vowel from the string. i am having problem in this code. It's not giving any error. and not working. can any1 help me with this code that what i am doing wrong in this?


#include <iostream>
using namespace std;
class mcchauhan{
public:
void enterTheString(string z){
x=z;
}
protected:
string x;
};
class vowel:public mcchauhan{
public:
string vowelFind(){
string finalString;

for (int m=0; m<=x.length();m++) {
if (x[m]=='a' || x[m]=='i' || x[m]=='u' || x[m]=='o' || x[m]=='u') {
x[m]=' ';
}else {
finalString =finalString + x[m];
}

}
return finalString;
}

};
int main(int argc, const char * argv[])
{
mcchauhan bo;
vowel mo;
bo.enterTheString("chauhan");
cout<<mo.vowelFind();
return 0;
}
Last edited on Aug 3, 2012 at 6:13am
Aug 3, 2012 at 6:45am
The reason it is not working correctly is because you are handling inheritance the wrong way. Even though mcchauhan is able to use vowels functions/protected variables that doesn't mean that the values from the class objects are shared between them.

In this case you create a mcchauhan class object called bo and pass a string into it. You then ask an mo object to work with that string even though for that particular object it has never been told what the string is.

You will want to use just 1 class object (In this case the vowel) and run all of the functions through that, it will still be able to access the string from the mcchauhan etc.

1
2
3
vowel mo;
mo.enterTheString("chauhan");
cout<<mo.vowelFind();


(On a side note for your vowel checker you have u listed twice and no e)
Aug 3, 2012 at 8:01am
Question,

Why check x[m] twice for 'u' ??
1
2
3
4
for (int m=0; m<=x.length();m++) {
if (x[m]=='a' || x[m]=='i' ||  x[m]=='u' || x[m]=='o' ||  x[m]=='u'   {
    x[m]=' ';
}


And James2250 is right, about why it won't work.
Last edited on Aug 3, 2012 at 8:03am
Aug 3, 2012 at 8:58am
here is an old forum link that might be useful to you..
http://www.cplusplus.com/forum/beginner/16889/
Aug 3, 2012 at 12:18pm
James2250:
thanks James2250. Yes its working now. but there is a problem if i am running this program, output is showing an extra reverse ? at the end. Can you throw some light on this? Output is like that "mynk chhn?"

#include <iostream>
using namespace std;
class mcchauhan{
public:
protected:
string x;
};
class vowel:public mcchauhan{
public:
string vowelFind(string x){
string finalString;

for (int m=0; m<=x.length();m++) {
if (x[m]=='a' || x[m]=='i' || x[m]=='u' || x[m]=='o' || x[m]=='e' || x[m]=='A' || x[m]=='I' || x[m]=='U' || x[m]=='O' || x[m]=='U') {
x[m]=' ';
}else {
finalString =finalString + x[m];
}

}
return finalString;
}

};
int main(int argc, const char * argv[])
{
vowel mo;
cout<<mo.vowelFind("mayank chauhan");
return 0;
}

Aug 3, 2012 at 12:56pm
it's a garbage value which generally occur due to memory leaks or improper memory assignment. You can see link that i posted and may you get some idea for loop.
Topic archived. No new replies allowed.