Click here to Skip to main content
15,915,508 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionRe: Removing tab spaces using C++ source code Pin
SandipG 27-Jun-08 2:52
SandipG 27-Jun-08 2:52 
AnswerRe: Removing tab spaces using C++ source code Pin
Roger Stoltz27-Jun-08 3:09
Roger Stoltz27-Jun-08 3:09 
AnswerRe: Removing tab spaces using C++ source code Pin
Nandu_77b27-Jun-08 3:11
Nandu_77b27-Jun-08 3:11 
GeneralRe: Removing tab spaces using C++ source code Pin
SandipG 27-Jun-08 3:18
SandipG 27-Jun-08 3:18 
QuestionRe: Removing tab spaces using C++ source code Pin
Nandu_77b27-Jun-08 3:28
Nandu_77b27-Jun-08 3:28 
QuestionRe: Removing tab spaces using C++ source code Pin
SandipG 27-Jun-08 3:32
SandipG 27-Jun-08 3:32 
AnswerRe: Removing tab spaces using C++ source code Pin
CPallini27-Jun-08 3:26
mveCPallini27-Jun-08 3:26 
AnswerRe: Removing tab spaces using C++ source code Pin
Roger Stoltz27-Jun-08 4:42
Roger Stoltz27-Jun-08 4:42 
As advised earlier by SandipG and cPallini, you have to replace the OR-operator (||) with an AND-operator (&&).
It does work.

However, what doesn't work is when the iterator reaches the end of the string; you'll get an assertion. If this is your new problem it would help if you mentioned that.

Try putting what you're trying to do in words, almost as psuedo-code.
This approach will make you write more understandable and intuitive code, which will make things easier for you and others that have to read your code.

I set the insert location and the current location to the beginning of the string.
Until I've reached the end of the string I continue to...
Check if the current char is a space or a tab and if it is, I skip the char, otherwise I copy the char to the insert location.


In code it would look something like this:
std::string& removeSpaces( std::string& str )
{
	std::string::iterator to = str.begin();
	std::string::iterator from = to;

	// Loop until we reach the end of the string
	while( from != str.end() )
	{
		// Check if the current char is a space OR a tab
		if( (*from == ' ') || (*from == '\t') )
		{
			// The current char is a space OR a tab, skip it!
			++from;
		}
		else
		{
			// The current char is NOT a space or a tab, copy it!
			*to++ = *from++;
		}
	}
	
	// If the iterator has not reached the end of the string, delete the rest
	if( to != str.end() )
	{
		str.erase( to, from );
	}

	// Return a REFERENCE to the same string we got as formal parameter
	// instead of making a copy of it on the stack
	// There's no point in returning a copy since the function operates
	// directly on the provided string buffer
	return str;
}



"It's supposed to be hard, otherwise anybody could do it!" - selfquote
"High speed never compensates for wrong direction!" - unknown


Questionusing transparent GIFs for toolbar buttons Pin
danginkgo27-Jun-08 1:25
danginkgo27-Jun-08 1:25 
AnswerRe: using transparent GIFs for toolbar buttons Pin
Iain Clarke, Warrior Programmer27-Jun-08 2:35
Iain Clarke, Warrior Programmer27-Jun-08 2:35 
GeneralRe: using transparent GIFs for toolbar buttons Pin
danginkgo27-Jun-08 3:59
danginkgo27-Jun-08 3:59 
QuestionGetDefID from CDialog in CView Pin
baerten27-Jun-08 1:23
baerten27-Jun-08 1:23 
AnswerRe: GetDefID from CDialog in CView Pin
Iain Clarke, Warrior Programmer27-Jun-08 2:49
Iain Clarke, Warrior Programmer27-Jun-08 2:49 
GeneralRe: GetDefID from CDialog in CView Pin
baerten27-Jun-08 3:09
baerten27-Jun-08 3:09 
Questionshowing a button with a bitmap Pin
VCProgrammer27-Jun-08 1:17
VCProgrammer27-Jun-08 1:17 
AnswerRe: showing a button with a bitmap Pin
SandipG 27-Jun-08 2:21
SandipG 27-Jun-08 2:21 
AnswerRe: showing a button with a bitmap Pin
sudhir_Kumar27-Jun-08 2:54
sudhir_Kumar27-Jun-08 2:54 
AnswerRe: showing a button with a bitmap Pin
Iain Clarke, Warrior Programmer27-Jun-08 3:01
Iain Clarke, Warrior Programmer27-Jun-08 3:01 
AnswerRe: showing a button with a bitmap Pin
David Crow27-Jun-08 4:43
David Crow27-Jun-08 4:43 
AnswerRe: showing a button with a bitmap Pin
Hamid_RT30-Jun-08 18:30
Hamid_RT30-Jun-08 18:30 
QuestionHow can set Focus on ListCtrl? Pin
Le@rner26-Jun-08 23:10
Le@rner26-Jun-08 23:10 
AnswerRe: How can set Focus on ListCtrl? Pin
Rajesh R Subramanian26-Jun-08 23:14
professionalRajesh R Subramanian26-Jun-08 23:14 
GeneralRe: How can set Focus on ListCtrl? Pin
Le@rner26-Jun-08 23:23
Le@rner26-Jun-08 23:23 
GeneralRe: How can set Focus on ListCtrl? Pin
Naveen26-Jun-08 23:35
Naveen26-Jun-08 23:35 
GeneralRe: How can set Focus on ListCtrl? Pin
Rajesh R Subramanian26-Jun-08 23:39
professionalRajesh R Subramanian26-Jun-08 23:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.