3

My problem is that I need to do a recursion for a string and change any e for an a. Every time I enter a word it only prints out the last letter.

My code so far:

string ReplaceEsWithAs(string s)
{
    if (s.length() == 1)
    {
        if (s == "e")
        {
            s = "a";

            return s;
        }

        else
        {
            return s;
        }
    }

    else
    {
        return ReplaceEsWithAs(s.substr(1));
    }
}
qxz
  • 3,814
  • 1
  • 14
  • 29
  • 1
    It always helps to format your code with proper indentation. – qxz Sep 22 '16 at 04:10
  • Sorry I'm completely new to this and I ll fix it. – Firesidemoney Sep 22 '16 at 04:13
  • I think that this problem would be much better solved by looping through (and possibly modifying) each character in the string, not with recursion. – qxz Sep 22 '16 at 04:14
  • Yea I can do it without the recursion but I want to know why its only printing out the last letter of the word. I don't know if it's my if statements because I tried with different ways of fixing it but it still doesn't work. – Firesidemoney Sep 22 '16 at 04:17
  • 1
    Try writing out a diagram of the program flow. Every `return` statement you have will return a single-character string; you probably mean to add the current result to the result of the recursive call, hence building the final output character-by-character. – qxz Sep 22 '16 at 04:19
  • I think I get what you mean. I see the problem now it's because of my first if statement it only returns if the string length is one. The else statement passes the rest of the letters in the string until there's one left. This is why I have been getting back one letter. Can you give me a hint of trying to figure out a better way to loop through the string one letter at a time. – Firesidemoney Sep 22 '16 at 04:24

2 Answers2

2

The only return statements in the function are under the condition

if (s.length() == 1)

It makes sense that the function return always returns a string with one character in it.

In the recursive part, you use:

return ReplaceEsWithAs(s.substr(1));

which calls the function with all but the first character of the string.

If you call with "abcd" from main, you call with "bcd" in the recursive call, then you call with "cd", then you call with "d", which returns "d", which is returned all the way.

You are just discarding the first character in every recursive call.

You need to use:

string ReplaceEsWithAs(string s)
{
   if (s.length() == 1)
   {
      if (s == "e")
      {
         return "a";
      }

      // No need for an else
      return s;
   }

   //  No need for an else.
   return ReplaceEsWithAs(s.substr(0,1)) + ReplaceEsWithAs(s.substr(1));
}
R Sahu
  • 204,454
  • 14
  • 159
  • 270
0

Here is an implementation just by looping through and mutating the string:

string ReplaceEsWithAs(string s) {
    for (size_t i = 0; i < s.length(); i++) {
        if (s[i] == 'e') {
            s[i] = 'a';
        }
    }
    return s;
}

You can access the individual chars in a string by index using []. This code uses a standard for loop to loop through each character. (size_t is an unsigned integer type that is generally used for indices.) For each char s[i], it checks if it's 'e', and if so, sets it to 'a'. Then it just returns the changed string.

This is better than a recursive approach because

  • it's easier to understand at a glance
  • it modifies the string in-place, meaning it doesn't have to do needless substring and concatenating (+)
  • it uses less memory
  • it won't cause the call stack to overflow for a long input string

Also, the standard library provides a function to do replace operations like this. See this answer.

Community
  • 1
  • 1
qxz
  • 3,814
  • 1
  • 14
  • 29