editing a string...

This is a discussion on editing a string... within the C++ Programming forums, part of the General Programming Boards category; hi all..........one question having declared a string like: Code: #include<string> using namespace std; string somestring = "blah blah blah"; is ...

  1. #1
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279

    editing a string...

    hi all..........one question

    having declared a string like:

    Code:
    #include<string>
    using namespace std;
    
    string somestring = "blah blah blah";

    is there a function in a <string> library that can edit this string so i can remove any occurance of a desired character....for instance if i would want to remove all the white spaces from it...???

    i looked in the MSDN Library but i'm not finding anything concrete
    ...........

    thanks in advance

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Interesting.

    One possible solution is STL's remove_if().

    Kuphryn

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    remove_if should work great like kuphryn said, but there is something to take notice of. You will also need to use the string's own erase member function along with using remove_if. If you use remove_if by itself, you will not get what you expected.

    Code:
    #include <string>
    #include <iostream>
    #include <algorithm>
    #include <functional>
    using namespace std;
    ...
    string data = "blah blah blah";
    // Remove all 'l' characters...
    remove_if(data.begin(),data.end(),bind2nd(equal_to<char>(),'l'));
    cout << data << endl;
    The output in this case is: bah bah bahlah. Notice the extra lah at the end. Combine the return value from the remove_if function object and the string's own erase member function to get the desired output.
    Code:
    #include <string>
    #include <iostream>
    #include <algorithm>
    #include <functional>
    using namespace std;
    ...
    string data = "blah blah blah";
    // Remove all 'l' characters...
    data.erase(remove_if(data.begin(),data.end(),bind2nd(equal_to<char>(),'l')),data.end());
    cout << data << endl;
    And now we get the desired output: bah bah bah
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    ResurgentBarbecue UnclePunker's Avatar
    Join Date
    May 2002
    Posts
    128
    You could just create a temporary string to take the input then search through it taking out and passing to another string every character apart from the ones you don't want.

    for example:
    Code:
    for (i=0; i<strlen(tempstring); i++)
    {
        if(i!=' ') //looking for white space
        {
            somestring[i] = tempstring[i];
        }
    }
    Compiler == Visual C++ 6.0
    "Come Out Fighting."

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >for (i=0; i<strlen(tempstring); i++)
    Using strlen as a loop condition should be avoided. For long strings you could slow execution quite a bit. Of course, a better way would be to use std::strings.

    >if(i!=' ') //looking for white space
    i is simply an index variable. You want to actually look in the string to see if the current character matches your search. Also, if you're looking for whitespace in general, isspace would be a better option.

    >somestring[i] = tempstring[i];
    somestring will have holes when you're done because you use i for both the source string and the destination string. Work it out on paper and you'll see that you need another index for somestring:
    Code:
    size_t len = strlen ( tempstring ) + 1; // Don't forget '\0'
    for ( size_t i = 0, j = 0; i < len; i++ ) {
      if ( !isspace ( tempstring[i] ) )
        somestring[j++] = tempstring[i];
    }
    My best code is written with the delete key.

  6. #6
    ResurgentBarbecue UnclePunker's Avatar
    Join Date
    May 2002
    Posts
    128
    Code:
    for (i=0; i<strlen(tempstring); i++)
    {
        if(tempstring[i]!=' ') //looking for white space
        {
            somestring[i-spacecount] = tempstring[i];
        }
        else
        {
            spacecount++;
        }
    
    }
    somestring[(strlen(somestring)+1)] = '\0';
    Compiler == Visual C++ 6.0
    "Come Out Fighting."

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Better, if a little cryptic. I'm not sure how many people will understand your indexing scheme immediately. Using strlen as a loop condition appears to be your style so I won't mention it again. But you still have a serious bug:
    Code:
    somestring[(strlen(somestring)+1)] = '\0';
    Tell me, when does strlen know when to stop? At the first '\0' character. Since you haven't correctly placed one, you're looking at the correct output followed by any amount of garbage. So you have two kinds of undefined behavior here:

    1) Acessing uninitialized memory locations.
    2) Potentially accessing memory outside of your address space.

    Also, you shouldn't add one to the result of strlen. Remember that strlen returns the number of characters in the string excluding the trailing '\0'. So the end of a valid C-style string is strlen ( s ) - 1, the location of the '\0' is strlen ( s ).
    My best code is written with the delete key.

  8. #8
    ResurgentBarbecue UnclePunker's Avatar
    Join Date
    May 2002
    Posts
    128
    Yes your right, it was just a piece of quick code but it could have ultimately been unhelpful, I will have to think before coding in future.

    I felt I needed to add the null terminator to the destination string as my loop condition is less than strlen not less than or equal to so it won't copy the null terminator, I just didn't do it correctly.

    Thanks.
    Last edited by UnclePunker; 02-26-2004 at 08:34 AM.
    Compiler == Visual C++ 6.0
    "Come Out Fighting."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 03:13 PM
  2. += operator
    By BKurosawa in forum C++ Programming
    Replies: 8
    Last Post: 08-05-2007, 03:58 AM
  3. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

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