push_back

I have a string which a user entered called "Input" and I am wanting to at some point add a 'k' onto the end. But when I use push_back, it adds a space between what was there before and the added 'k'. How do I get rid of that extra space? And I am also trying to erase the first pair of double letters but it is only deleting one of them.

void addktoend(int i, string &Input)
{
cout << "Add an k to the end" << endl;
Input.push_back('k');
cout << Input << endl;
}

void removedoubles (int i, string &Input)
{
cout << "Remove the first pair of double letters" << endl;
for (i = 0; i< Input.length() ; i++)
{
if (Input[i] == Input[i+1])
{
Input.erase (i, 1 ) ;
Input.erase ((i + 1 ) , 1 ) ;
break;
}
}
cout << Input << endl;
}
Last edited on
string::push_back() should work fine. Can you post the code how you input the string?

Btw, use code tags [code][/code].


Just spot something in removedoubles():
you may probably read pass the string (when i = Input.length()-1). You only have to check up to the second last character.
Last edited on
void enterString(string &Input)
{
cout << "Enter a string: ";
getline(cin, Input);
cout << Input << endl;
}



ok, and now whenever I run the program it stops when it gets to the following function:

void pafterh(int i, string &Input)
{
cout << "Insert a 'p' after every 'h'" << endl;
for (i = 0 ; i < Input.length() ; i++)
{
if (Input[i] == 'h')
{
Input.insert ('p', Input.substr((i+1), 1) );
}
}
cout << Input << endl;
}
Last edited on
Topic archived. No new replies allowed.