Unmash a string in C++ using recursion

In summary, the user is trying to solve part B of a homework equation, but doesn't know how to do it.
  • #1
Hughng
26
0

Homework Statement


MUST USE RECURSION TO SOLVE THESE PARTS.

Part A: Have a user input a string. Then display this string smashed up as follows: display the first character in the string, then the last, then the second, then the second to last, then the third... So if the string is “abcdef”, it will display:
afbecd (input “abcdef”)
12345 --> 15243
123456 --> 162534

Part B: Now, unmash the above strings.
i.e 162534 -->123456

Homework Equations

The Attempt at a Solution


I got part A to work.
Code:
#include <iostream>
using namespace std;
void mash(string s);

int main()
{
    string sequence;
    cout << "Enter a sequence: ";
    getline(cin, sequence);
    mash(sequence);
}

void mash(string s)
{
    int a = s.length();
  
    if (a == 0)
    {
    return;
    }
    if (a == 1)
    {
    cout << s;
    return;
    }
  
    cout << s[0];  
  
    if(a>1)
    {          
        cout << s[a - 1];  
        s = s.substr(1,a-2);
        mash(s);
      
    }
}
but I have no clue how to approach part B. I guess I can try to print out the characters in the even position, say in the string 162534, thus I will get 123. Then I guess I can try to print out the odd position characters from the last one up to the first one, i.e, 456. Combining these two will get the original strings but I have no clue how to use recursion to solve part B.
 
Last edited by a moderator:
Physics news on Phys.org
  • #2
You can use recursion in the sub-steps: one recursion for the even characters, one for the odd characters.
Or unmash the string in the code with a recursive algorithm (2 characters at a time?), then print the whole string.
 
  • #3
Hughng said:
Combining these two will get the original strings but I have no clue how to use recursion to solve part B.
I'm looking forward to seeing the code you come up with to solve this. We can't help with improvements to your algorithm until you have first put in a good attempt by yourself.

Recursive routines pack a lot of power into few lines, so it's worth investing time in getting them right.
 

1. What is recursion in C++?

Recursion in C++ is a programming technique where a function calls itself repeatedly until a certain condition is met. This allows for solving complex problems by breaking them down into smaller, simpler problems.

2. How does recursion work in C++?

In recursion, a function calls itself with a smaller input each time, until it reaches a base case where the function stops calling itself and returns a value. This value is then used in the previous function call, and so on until the original function call is completed.

3. What is the base case in recursion?

The base case in recursion is a condition that stops the function from calling itself and allows it to return a value. This is necessary to prevent the function from entering an infinite loop.

4. How do you unmash a string in C++ using recursion?

To unmash a string in C++ using recursion, the function takes in a string as input and checks if it is the base case (i.e. the string is empty). If not, it calls itself with a substring of the original string and appends the first character of the original string to the end of the substring. This process is repeated until the base case is reached and the unmashed string is returned.

5. What are the advantages of using recursion in C++?

Recursion allows for solving complex problems in a concise and elegant manner. It also simplifies the code by breaking it down into smaller parts, making it easier to debug and maintain. However, it is important to use recursion carefully as it can lead to stack overflow if not implemented correctly.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
932
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
14
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
2K
Back
Top