944,000 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 313
  • C++ RSS
Nov 5th, 2009
0

Having trouble with deleting suffixes and need help

Expand Post »
i need help with removing "s" and "tion" properly if i want to input "composition" it should leave me with "composi" but it leaves me with "compo" and if i were to enter a word that starts with an s and doesnt end with an s the word would not be shown. on the other hand if i were to start and end a word with s the s at the end would be deleted like it should. help if you can
[CODE]


int main()
{
cout<<"Enter a series of strings.\n";
int end,end2,end3,end4;

string input;

while(cin>>input)
{

end=input.rfind("ing");
if(end>-1)
{
input=input.substr(0,end);
// cout<<input<<"\n";
}
end2=input.rfind("ed");
if (end2>-1)
{
input=input.substr(0,end2);
// cout<<input<<"\n";
}
end3=input.rfind("s");
if (end3>-1)
{

input=input.substr(0,end3);
//cout<<input<<"\n";
}

end4=input.rfind("tion");
//string last_pos=end4.rfind("s");
if(end4>=-1)
{

input=input.substr(0,end4);
// cout<<input<<"\n";
}

cout<<input<<"\n";
}

}
[CODE]
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
reese27 is offline Offline
10 posts
since Sep 2009
Nov 5th, 2009
0
Re: Having trouble with deleting suffixes and need help
For removing just a single character you could do something like..

C++ Syntax (Toggle Plain Text)
  1.  
  2. for (int i = 0; i < string.size(); i++)
  3. {
  4. if (string[i] == 's')
  5. continue;
  6. else
  7. substring += string[i];
  8. }
Reputation Points: 11
Solved Threads: 8
Light Poster
Kontained is offline Offline
34 posts
since Sep 2009
Nov 5th, 2009
0
Re: Having trouble with deleting suffixes and need help
Close on the code tags. Note the / (slash) on the ending tag:


[code]
// code here
[/code]


You need to find "s" OR find "ing", not both. If you have "standing", you want to end up with "stand", right? So once you find, "ing" and delete it, make sure you do not test for "s".

C++ Syntax (Toggle Plain Text)
  1. string word = "standing";
  2. bool foundIngPrefix = false;
  3.  
  4. // code
  5. if (/* found "ing" prefix */)
  6. {
  7. // delete "ing" prefix.
  8. foundIngPrefix = true;
  9. }
  10.  
  11. if (!foundIngPrefix)
  12. {
  13. // test for and delete "s" if needed.
  14. }

That'll solve the problem of accidentally finding both, but I think you have another problem. How about a word like "singer"? It has "ing" inside of it, but it doesn't END with it. rfind is going to find it and then you'll delete it. If the "ing" must be at the END, you'll need to add at least one more test before deleting.
Last edited by VernonDozier; Nov 5th, 2009 at 6:42 pm.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: structures with pointers
Next Thread in C++ Forum Timeline: coding help, reverse array, check for palindrome





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC