Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

****having trouble getting this recursive method to work: not getting any compil

ID: 3875273 • Letter: #

Question

****having trouble getting this recursive method to work: not getting any compilation errors but the program fails****

int main() {
   string lengthString;


   cout << " Please enter a string to be concatenated: " << endl;
   cin >> lengthString;
   string addon = ", Yo Mamma!";
   char* addonChars = (char*)addon.c_str();
   char* baseString = (char*)lengthString.c_str();

   ConcatenateStrings(baseString, addonChars);
   cout << "Thank you " << "Answer= " << lengthString << endl;

   return 0;
}

void ConcatenateStrings(char* s1, char* s2)
{
   // dereferenced s1 pointer is, once s1 has been iterated through, is copied from s2 recursively till zero is finally given.

   (*s1) ? ConcatenateStrings(++s1, s2) : (*s1++ = *s2++) ? ConcatenateStrings(s1, s2) : 0;
}

Explanation / Answer

#include <iostream>
#include <string.h>
using namespace std;
void ConcatenateStrings( char*, char*);
int main() {
string lengthString;

int len=0;
cout << " Please enter a string to be concatenated: " << endl;
cin >> lengthString;
string addon = ", Yo Mamma!";
char* addonChars = (char*)addon.c_str();
char* baseString = (char*)lengthString.c_str();
len = strlen(baseString);
ConcatenateStrings( baseString + len , addonChars );
cout << "Thank you " << "Answer= " << lengthString << endl;

return 0;
}

void ConcatenateStrings(char* s1, char* s2)
{
// dereferenced s1 pointer is, once s1 has been iterated through, is copied from s2 recursively till zero is finally given.

/* (*s1) ? ConcatenateStrings(++s1, s2) : (++*s1 = ++*s2) ? ConcatenateStrings(s1, s2) : (void) 0;
*/
    *s1++ = *s2++;
(*s2)? ConcatenateStrings(s1, s2 ) : (void)0;
}