Unmash a string in C++ using recursion

Click For Summary
SUMMARY

The discussion focuses on solving a string manipulation problem in C++ using recursion. Part A requires users to input a string and display it in a specific "mashed" order, while Part B involves "unmashing" the string back to its original form. The provided code successfully implements Part A, demonstrating the use of recursion to alternate between the first and last characters. For Part B, participants are encouraged to utilize recursion to extract even and odd indexed characters separately to reconstruct the original string.

PREREQUISITES
  • Understanding of C++ syntax and structure
  • Familiarity with recursion concepts in programming
  • Knowledge of string manipulation techniques in C++
  • Experience with input/output operations in C++
NEXT STEPS
  • Implement a recursive function to extract even indexed characters from a string in C++
  • Develop a recursive function to extract odd indexed characters from a string in C++
  • Combine the results of the even and odd character functions to reconstruct the original string
  • Explore advanced recursion techniques and their applications in C++ programming
USEFUL FOR

C++ developers, computer science students, and anyone interested in mastering recursion and string manipulation techniques in programming.

Hughng
Messages
26
Reaction score
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;
    count << "Enter a sequence: ";
    getline(cin, sequence);
    mash(sequence);
}

void mash(string s)
{
    int a = s.length();
  
    if (a == 0)
    {
    return;
    }
    if (a == 1)
    {
    count << s;
    return;
    }
  
    count << s[0];  
  
    if(a>1)
    {          
        count << 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
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.
 
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.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
4
Views
2K
  • · Replies 14 ·
Replies
14
Views
5K
  • · Replies 7 ·
Replies
7
Views
2K