How is this accomplished? I am using the string class ie
string test;
cin >> test; //they enter " This is a stri ng "
*stripping process*
//test now reads "Thisisastring"
Printable View
How is this accomplished? I am using the string class ie
string test;
cin >> test; //they enter " This is a stri ng "
*stripping process*
//test now reads "Thisisastring"
If you must know I HAVE looked around for a few hours now and have attempted it myself multiple times; I am not just asking this without any effort
I'm not saying this is the best way to do this... I did an assignment like that a while ago and i used getline to input the string into an array. Then i just used a loop to copy every character that is not a space to a new array. Works fine. Don't forget the '\0' character at the end of the new string....
I tried this but it didn't work
Code:string stripSpace(const string& infix){
string strippedString;
for (int i = 0; infix[i] != '\0'; i++){
if (infix[i] != ' ')
strippedString += infix[i];
}
return strippedString;
}
I have never really worked with the string class before, so I can't really help you out there. If you want to use an array though, you could do it like this:
[code]
char rawinput[80];
char input[80];
cin.getline(rawinput, 80, '\n');
for(int i = 0, j = 0;;i++) {
if (rawinput[i] == ' ');
else{
input[j] = rawinput[i];
j++;
}
if (rawinput[i] == '\0') break;
}
[\code]
I can do it with arrays of chars but it's the string class that's baffling me
Sorry then, i am doing a hw assignment with strings right now, and i am a bit baffled myself...
thank you, though
Code:string::iterator iter = test.begin();
string::iterator end = test.end();
while(iter != end)
{ if(*iter == ' ')
{ test.erase(iter,iter+1);
}
iter++;
}
Code:int main()
{
string str = "blah blah blah";
cout<<str<<endl;
int x = str.find(" ");
while(x < str.length())
{
str.replace(x, 1, "");
x = str.find(" ", x + 1);
}
cout<<str<<endl;
return 0;
}
I also realized something. I am not inputting my string correctly.
cin >> string; takes to the first space
cin.getline doesn't work
Ahhhh!
But BTW thanks you guys, that DOES work for removing whitespace
Thank all of you so much for the help!!