Sorry to bother you all with such trivial stuff, but I just finished about 35 years of work in construction, all over the US and Europe. And when the market went south I went back to school. Calc and trig are going fine, but C++ is kicking my butt. We went from “Hello World to arrays, to structs, to classes, to vectors, to pointers, to namespaces and I/O streams, and I’m still spinning. I PROMISE to only ask for help on a topic once. And for every take, I will provide a put. I don’t have a big bag of puts yet, but every time y’all help me, I get better…it’s just a matter of time until I can be as helpful to others as you’s are to me now. Thanks in advance.

For now:

What I need is a good function for taking out the white space.
I need to edit and correct a string of class string. I commented out a lot because I am temporarily lost. My main focus is to remove whitespace. I think the “toupper” and the rest will come to me fine, but I can’t get how to find, skip, and remove, or copy the corrected version to str2.
Please help.

#include <iostream> 
	#include <string>
	#include <cctype>
	using namespace std;

	//string removeSpaces(string s1, string& s2);

//	void swap(char& v1, char& v2);
	
	int main()
	{
	    string str1(" the    Answer  to life, the universe   and everything IS  42.");
		string str2;
	    //cout << "Enter a sentence to be corrected\n"
	      //   << "followed by pressing Return.\n";
	    //cin.getline(str1, 100);
		cout << endl;
		cout << " You entered: \n\n";
	 
		cout << str1 <<endl;
		cout << endl;
		//removeSpaces(str1[100], str2[100]);
		cout << endl;
		//cout << " The corrected version is: \n";
		cout << str1 << endl;
		//cin.getline(str2, 100);
		cout << endl;
		//cout << str2 << endl;

	    return 0;
	}

/*string removeSpaces(string s1, string& s2)  
{ 
	int j = 0;
	
 for(int i = 0; i < 100; i++)
 {
	
     if(s1[i] == ' ') 
		 ++j;
			if(j > 1)
				s1 = 'x';
				//s2[i] = s1[i + 1];
			
}
 return s1; 
}*/ 

/* void swap(char& v1, char& v2)
	{
	    char temp = v1;
	    v1 = v2;
	    v2 = temp;
	}*/

Recommended Answers

All 8 Replies

I think the easiest way is, create another string and copy the original(minus the spaces) into it and then assign the temp to the original.

it doesn't seem like you understand the string class so here's a link:

http://www.cplusplus.com/reference/string/string/

take note of size(), push_back() and the operators

and as stated, i would create a temp string, read/copy the original then return the temp

I'm on it. Will read this and some more links on the way. Sure to resolve today. Thanks

hey,make a temporary string and use an if condition to check for whitespace.do a character by character search on the string,for its lenth.you can get the length of the string by strlen() function.

int length= strlen(str1);

if you get a whitespace then dont copy the character to the temporary string.otherwise do!...

Happy coding.

Another helpful suggestion. I'm getting real close now. By next week, I'll try to be the king of string so that I'll be able to give something back...well at least a court jester.

commented: Keep on keeping on! +0

lol....well,thats the spirit!

Good luck :)

If you have some familiarity with standard algorithms:

std::string str = " string \twith \n ws." ;
    str.erase( std::remove_copy_if( str.begin(), str.end(), str.begin(), ::isspace ),
               str.end() ) ;

Helpful tidbits. I'm implementing this and gaining more control and knowledge. Priceless. I mean to a wanna be geek.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.