LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-14-2006, 01:55 PM   #1
babyboss
Member
 
Registered: Sep 2004
Posts: 124

Rep: Reputation: 15
C++ little programming question


Hello, I am trying to figure out a way to remove whitespace inside a string. Can anyone help me a little on this.
Example: input:ha ha ha
output:hahaha

I manage to only remove one space inside a string
like: input:ha ha ha
output:hahaha
Following are the code I have gotten so far:
#include <iostream>
#include <string>
using namespace std;


int main()
{
string input;
int i = 0;
int space_appearance = 0;

getline(cin, input, '\n');

for(i = input.find(" ", 0); i !=(int)string::npos; i=input.find(" ", i))
{
input.erase(i, 1);
space_appearance++;
i++;
}


cout << input << endl;
cout << space_appearance << endl;
}



I would really appreciate for anyhelp, thank you very much!
 
Old 01-14-2006, 02:30 PM   #2
paragn
Member
 
Registered: Jan 2006
Distribution: Red Hat EL5, Fedora 7
Posts: 259

Rep: Reputation: 30
hi
its working code. for input a b c d
i got output abcd 3
 
Old 01-14-2006, 02:36 PM   #3
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Is this homework?

"_" denotes spaces for easy reading
input:ha_ha_ha
output:hahaha correct

input:ha_ha__ha
output:haha_ha incorrect

input:__ha_ha_ha
output:_hahaha incorrect

As you can hopfully see from the output you are not far away from getting what you want but there is a logical error here. Can you see what it is?

As a side note please include "return 0;" at the end of your main func as it should return an int.

<edit> maybe '_' wasnt the best char to use to show the error, does this make it clearer.
'/' denotes space
input:ha/ha/ha
output:hahaha correct

input:ha/ha//ha
output:haha/ha incorrect

input:////ha/ha/ha
output://hahaha incorrect

Last edited by dmail; 01-14-2006 at 03:01 PM.
 
Old 01-14-2006, 03:12 PM   #4
babyboss
Member
 
Registered: Sep 2004
Posts: 124

Original Poster
Rep: Reputation: 15
dmail, I see the logical error here. The loop only interate the string once, and it only delete one character at a time. I was stuck on this as well, had a hard time to get over wtih it.
I am able to correctly delete whitespace in front and after the string, but not inside the string.

Last edited by babyboss; 01-14-2006 at 03:15 PM.
 
Old 01-14-2006, 03:26 PM   #5
babyboss
Member
 
Registered: Sep 2004
Posts: 124

Original Poster
Rep: Reputation: 15
haha dmail!!!
I got it
stupid "i++"!!!!!
thank you for your hints.!!
 
Old 01-14-2006, 03:33 PM   #6
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Quote:
Originally Posted by babyboss
haha dmail!!!
I got it
stupid "i++"!!!!!
thank you for your hints.!!
Well done. I didnt want to just give you the answer.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
a little programming question haora Programming 3 06-24-2005 08:19 PM
Question about programming jcramer Mandriva 2 02-01-2004 02:22 PM
C Programming Question Makaveli.2003 Programming 4 03-03-2003 06:34 AM
Programming question pk21 Linux - Newbie 6 12-02-2002 01:17 PM
Programming question LinuxQuest01 Linux - Software 3 08-09-2002 11:42 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:22 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration