Solved

Easy String question

Posted on 2004-08-18
5
239 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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 viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

863 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

24 Experts available now in Live!

Get 1:1 Help Now