Having trouble with deleting suffixes and need help

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2009
Posts: 10
Reputation: reese27 is an unknown quantity at this point 
Solved Threads: 0
reese27 reese27 is offline Offline
Newbie Poster

Having trouble with deleting suffixes and need help

 
0
  #1
25 Days Ago
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]
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 19
Reputation: Kontained is an unknown quantity at this point 
Solved Threads: 5
Kontained Kontained is offline Offline
Newbie Poster
 
0
  #2
25 Days Ago
For removing just a single character you could do something like..

  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. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,819
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster
 
0
  #3
25 Days Ago
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".

  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; 25 Days Ago at 6:42 pm.
Reply With Quote Quick reply to this message  
Reply

Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC