Solved

Easy String question

Posted on 2004-08-18
5
237 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 300 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 200 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now