Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Easy String question

Posted on 2004-08-18
5
Medium Priority
?
247 Views
Last Modified: 2010-04-01
I'm trying to remove some title from the end of a string (phd, M.D. ESQ.)  

and in my debugging process, I am totally confused.  my goal is to remove the string listed in cleantTitle from the end of a string (there can be an extra character at the end of the string).

on removeEnding("PhD"), the index found is 6  which is the END of the second phd.  

1.  Why is Phd matching when it is not in the right case.

2.  Why is it giving me the end of the phd index?

on removeEnding("Phd") called right afterwars, the index found is 1.  which corresponds to nothing, what is going on?

CODE:

void main()
{
      static std::string name("phd phd");
      cleanTitle(name);
}

where

void cleanTitle ( string& str)
{
      cout << "cleanTitle()" << endl;
      removeEnding(str, "Phd");
      removeEnding(str, "PhD");
      removeEnding(str, "M.D.");
      removeEnding(str, "MD");
      removeEnding(str, "test");
}

void removeEnding(string &str, string ending)
{
      cout << "removeEnding()" << endl;
      cout << str.c_str() << endl;
      size_t index = str.find_last_of(ending);
      if(index != string::npos)
      {
            if(index + 2 >= str.size())
            {
                  str.erase(index - ending.size() + 1, ending.size());
            }
      }
      cout << str.c_str() << endl;
}

0
Comment
Question by:jjacksn
  • 2
  • 2
5 Comments
 
LVL 3

Accepted Solution

by:
Tyrsis earned 1200 total points
ID: 11837050
Hello,

find_last_of() returns the index of ANY character specified in the string.  I think you want to replace find_last_of() with rfind().

Tyrsis
0
 
LVL 15

Expert Comment

by:efn
ID: 11837497
Also, rfind will give you the index of the beginning of the substring in the searched string, so you will need to revise the index + 2 and index - ending.size() + 1 expressions.

--efn
0
 
LVL 5

Author Comment

by:jjacksn
ID: 11837559
does find do that as well?
0
 
LVL 5

Author Comment

by:jjacksn
ID: 11837563
also, why is it "finding" a string with the wrong case?
0
 
LVL 15

Assisted Solution

by:efn
efn earned 800 total points
ID: 11837861
find finds the first occurrence of the substring.  rfind finds the last occurrence.

It's not finding a string with the wrong case, it's finding a character with the right case.  When str is "phd phd" and ending is "Phd" and you call str.find_last_of(ending), you are not telling it to find the string "Phd".  You are asking for last position in str that contains a 'P' or an 'h' or a 'd'.  The last such position is 6, which contains a 'd'.

--efn
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Suggested Courses

963 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question