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

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";
   }

}

Recommended Answers

All 2 Replies

For removing just a single character you could do something like..

for (int i = 0; i < string.size(); i++)
      {
               if (string[i] == 's')
                          continue;
               else
                          substring += string[i];
      }

Close on the code tags.

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".

string word = "standing";
bool foundIngPrefix = false;

// code
if (/* found "ing" prefix */)
{
   // delete "ing" prefix.
   foundIngPrefix = true;
}

if (!foundIngPrefix)
{
  // test for and delete "s" if needed.
}

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.