0

I'm making a function that removes elements from a string. However, I cant seem to get both of my loops to work together. The first while loop works flawlessly. I looked into it and I believe it might be because when "find_last_of" isn't found, it still returns a value (which is throwing off my loop). I haven't been able to figure out how I can fix it. Thank you.

#include <iostream>
#include <string>

using namespace std;

   string foo(string word) {
      string compare = "!@#$";
      string alphabet = "abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

    while(word.find_first_of(compare) < word.find_first_of(alphabet)) {
        int position = word.find_first_of(compare);
        word = word.substr(++position);
    }
           while(word.find_last_of(compare) > word.find_last_of(alphabet)){
              int size = word.length();
              word = word.substr(0, --size);
         }  
    return word;
}

int main() {
    cout << foo("!!hi!!");

    return 0;
}

I wrote it like this so compound words would not be affected. Desired result: "hi"

1
  • The value that find_last_of returns if it could not find a match is called npos and is unsigned with value of -1 (which converts to whatever maximum value of size_type is. Either check for it specifically and then do the comparison, or do refactor a bit. Commented Feb 16, 2014 at 20:26

2 Answers 2

1

It's not entirely clear what you're trying to do, but how about replacing the second loop with this:

string::size_type p = word.find_last_not_of(compare);
if(p != string::npos)
  word = word.substr(0, ++p);
Sign up to request clarification or add additional context in comments.

3 Comments

Sorry, the result I'm looking for is "hi" (without any extra characters).
@user2780598: this will do it.
That it will, much appreciated!
0

It's not clear if you just want to trim certain characters from the front and back of word or if you want to remove every one of a certain set of characters from word no matter where they are. Based on the first sentence of your question, I'll assume you want to do the latter: remove all characters in compare from word.

A better strategy would be to more directly examine each character to see if it needs to be removed, and if so, do so, all in one pass through word. Since compare is quite short, something like this is probably good enough:

// Rewrite word by removing all characters in compare (and then erasing the
// leftover space, if any, at the end).  See std::remove_if() docs.
word.erase(std::remove_if(word.begin(), 
                          word.end(),
                          // Returns true if a character is to be removed.
                          [&](const char ch) {
                            return compare.find(ch) != compare.npos;
                          }),
           word.end());

BTW, I'm not sure why there is both a compare and alphabet string in your example. It seems you would only need to define one or the other, and not both. A character is either one to keep or one to remove.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.