GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 21-Jan-2009, 13:07
TransformedBG TransformedBG is offline
Member
 
Join Date: Oct 2006
Posts: 117
TransformedBG is on a distinguished road

Help with removing spaces...


basically all i want to do is remove the spaces in a word for instance user inputs < bob > i want it to look like <bob>.

CPP / C++ / C Code:
if(tokenOne *has a space in it* = ' '){
    string removeSpaces;
    
    for(int i = 0; i < size; i++){
       if(tokenOne[i] = ' '){};
           tokenOne[i] << endl;
           removeSpaces[i] = tokenOne[i];
        }
    cout << removeSpaces << endl;
    return 2;
}

This should work but its not.. or is there and easier way to do this?
  #2  
Old 21-Jan-2009, 13:21
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 713
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Help with removing spaces...


I always find that creating a more generic function that is reusable might be the best solution. I'm thinking something like:

CPP / C++ / C Code:
// Replace every occurrence of string s2 with string s3 within string s1 and return the new string
string FindAndReplace( string s1, string s2, string s3)
{
  string result = "";
  int i;

  while( s1.length() > 0 )
  {
    // If s1 starts with s2, remove s2 from the front of s1 and append s3 to result

    // else append the first character of s1 to result
  }

  return result;
}
There might be a faster way to do this such as searching for the first occurrence of s2 within s1 in each loop but this is a simple solution. Also, you would be advised to pass const references instead like: string FindAndReplace( const string& s1, const string& s2, const string& s3 );

Using this generic function, you could call it like:
CPP / C++ / C Code:
string empty = "";
string input = "< bob >";
string space = " ";
string spacesRemoved = FindAndReplace( input, space, empty );
  #3  
Old 21-Jan-2009, 13:34
TransformedBG TransformedBG is offline
Member
 
Join Date: Oct 2006
Posts: 117
TransformedBG is on a distinguished road

Re: Help with removing spaces...


see there is logical thinking then there is me lol.. thanks man ill give that a shot
  #4  
Old 21-Jan-2009, 19:33
Mexican Bob's Avatar
Mexican Bob Mexican Bob is offline
Member
 
Join Date: Mar 2008
Location: Chicxulub, Yucatán
Posts: 226
Mexican Bob is a jewel in the roughMexican Bob is a jewel in the roughMexican Bob is a jewel in the rough

Re: Help with removing spaces...


Write what you need. It is usually quicker, easier and...BTW...does what you need. If you want to write something that handles all cases of what you may need in the future, good luck and have fun trying to reach that pinnacle.

CPP / C++ / C Code:
#include <iostream>
#include <string>

void remove_spaces(std::string& str)
{
    size_t pos = str.find_first_of(" ");
    while(pos != std::string::npos)
    {
	str.erase(pos, 1);
	pos = str.find_first_of(" ");
    };
}

int main()
{
    std::cout << "Enter a string: ";
    std::string s;
    getline(std::cin, s);
    remove_spaces(s);
    std::cout << "Result of removing spaces: " << s << std::endl;

    return 0;
}

Output:

Code:
$ ./rmsp Enter a string: ab cd Result of removing spaces: abcd $ ./rmsp Enter a string: abc def Result of removing spaces: abcdef $ ./rmsp Enter a string: This is not the end of the world as we know it. Result of removing spaces: Thisisnottheendoftheworldasweknowit. $ ./rmsp Enter a string: < bob > Result of removing spaces: <bob>


MxB
  #5  
Old 24-Jan-2009, 18:24
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 1,032
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough

Re: Help with removing spaces...



Please stick to criticizing code and don't make personal attacks.
Do NOT try to turn the forums into a battle field. Solve your problems in private, if you have any.
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
 
 

Recent GIDBlogOnce again, no time for hobbies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Replace multiple white spaces with a single white space in a string abhisheknayak C++ Forum 4 29-Feb-2008 02:40
Strings ( Spaces between words ) Max_Payne C++ Forum 2 21-Oct-2007 16:09
removing excess space's dave88 C++ Forum 10 17-Oct-2005 18:28
Problems reading strings with spaces monalin C++ Forum 3 14-Oct-2005 21:32
MATCH text without spaces ScreamingWeasel MySQL / PHP Forum 2 11-Jun-2004 21:47

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 09:36.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.