How to delete character from string?


I am only a few weeks into my course, and we only covered strings and loops so far. No advanced concepts please.

Here is the method I have so far. The user inputs a letter and a string, and I want for the new string to have the original string that the user entered, but without the letter.

For example:
User enters character: h
User enters string: superhero

Then the new string must be: superero

What I have got so far, I am stuck....

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
string removeLetter( string answer_string, char answer_char)
{
   char tempString, tempString2;
   int length;
   

   length = answer_string.length();
   for (char p = 0; p < length; p++)
   {
      tempString = answer_string[p];

      if (tempString != answer_char)
      {
         cout << tempString;        
      }
    
           
   }
   return ???;
}

I tried experimenting for hours, but can't figure it out... so any help will be appreciated.
Last edited on
is this a school assignment? there are multiple way to do this, but you'll want to solve this through your current knowledge.
Yes, but I have spent over a day on this (I have already completed the other parts of the assignment). Any help or even tips would be appreciated.
have you learned any functions of the string library? e.g. substr()
So far, all the checking looks right. There is a string.erase function that you can look into that should help you.
Nope, just the very basic like strings, string concatenation, if/else, loops,...

well how about this...

once you find where h is, concatenate the string up to the h and after the h. After this add the two strings together back into the original string.
We haven't learned string.erase so I don't think we can use that.

Thanks Need4Sleep, I'll look into that.
Actually, we don't know what character or string the user will enter, so it could be anything.

That's where I'm stuck...
Well I can do it with using str.erase but if you can't use it then I'm not sure what else you can do.
There is method erase that allows to remove characters from a string.

basic_string& erase(size_type pos = 0, size_type n = npos);

So the algorithm is the following. You find in a loop fhe given letter and remove it. Then again in the next iteration of the loop try to find the same letter and remove it until it would not be found

To find a letter use the following method of the class string

size_type find(charT c, size_type pos = 0) const noexcept;
easy solution: use the erase function of std::string

otherwise:

a string is a character array, that allows you to get the single characters via their index [i].
just copy the part till the letter you want to delete and then overwrite each letter with the next one -> replace [i] with [i+1]
well, using the string lib this is fairly simple actually.

create a temp string in the function
loop through the length of the function, starting at the first element. If the element is not equal to answer_char, add it onto the temp string. If it is answer_char, do nothing. return temp

This will give you the string that is missing the answer_char letter.
@continuume

If you're working with loops and string concatentation, then the code you posted initially is within one line (more or less) of working.

Edit: one line + a handful of corrections!

All you have to do is *not* concatenate the char you don't want when you copy (char by char).

Andy

PS See "How to use tags" for how to format your code
http://www.cplusplus.com/articles/z13hAqkS/
Last edited on
Okay, I'm still stuck, guys. I can get it to output correctly if the cout statement is within the method, but I can't get this value to return to the main correctly (can't return a char, but strings?)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
string removeLetter( string answer_string, char answer_char)
{
   char tempString, tempString2;
   int length;
   

   length = answer_string.length();
   for (char p = 0; p < length; p++)
   {
      tempString = answer_string[p];

      if (tempString != answer_char)
      {
         cout << tempString;        
      }
    
           
   }
   return ???;
}
Well, the thing which is missing from the above code is somewhere to put the result. Hence the difficulty on line 19.
How about adding at line 5 string resultString;

Then instead of using cout at line 14, add the current character to resultString.
That should do the trick.

(there are at least three ways to add a character to the end of a string, using += or append() or push_back(). I'd use the first option).
see http://www.cplusplus.com/reference/string/string/

There are a few things that are either wrong or bad practice with the above code,
tempString and tempString2 are badly-named, they are not strings at all, and should not be named as such.

The variable p in the for loop is of type char, when it would be much better to use an int. The use of char p means the code will fail if the input string is longer than 127 characters.

Last edited on
Topic archived. No new replies allowed.