String

This is a discussion on String within the C++ Programming forums, part of the General Programming Boards category; How should i implement a string that will return all the contents of it after removing all of its whitespaces. ...

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    1

    String

    How should i implement a string that will return all the contents of it after removing all of its whitespaces. For example, if my string is "aa bb ab", then it should return "aabbab" after removing the whitespaces. What should be the algorithm be ?

  2. #2
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    you can use two loops, one will traverse the char array 1 char at a time and the other will check for spaces and skip over them. This method is best cos it doesnt require a second array to copy the data to.

    Have a try and if you cant get it I'll give a little more help.

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    101
    Code:
    #include <string>
    #include <algorithm>
    #include <cctype>
    
    using namespace std;
    
    string remove_whitespace(string s)
    {
        s.erase(remove_if(s.begin(), s.end(), &isspace), s.end());
        return s;
    }
    - lmov

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. compare structures
    By lazyme in forum C++ Programming
    Replies: 15
    Last Post: 05-28-2009, 03:40 AM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 05:30 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 07:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21