3

Just for full disclosure, this is a homework assignment.

I need to reverse a char string using a recursive function that accepts only a "char *string" input

I've done a lot of googling and whatnot, but just can't seem to figure it out. If I could use a void function, this would be done hours ago, but alas, that's not allowed.

The following code returns nothing, we're not allowed to change the function signature either

char *reverseString(char *string)
{

    if (*string =='\0') return string;

    else
    {
        return reverseString(string + 1) + *string;
    }

}

this is my multiple attempts at calling this function from the main to get output from it, and though the code runs, I get no output in the console

char Tstring[] = "TestString";
cout << reverseString(Tstring);
cout << *reverseString(Tstring);
char *answer2 = reverseString(Tstring);
cout << *answer2;

I'm not asking for a direct solution obviously, but I'm having trouble wrapping my head around how to do this using a pointer as an input and whatnot. Any tips or nudges in the right direction are appreciated

FyrusDak
  • 41
  • 1
  • 3
  • 1
    "Write `void` function", then change it to return "`char *`" and return the input. – Joker_vD Oct 26 '15 at 09:25
  • are you not allowed to use other functions to do the work? – Tom Tanner Oct 26 '15 at 09:27
  • 6
    `char*` strings are not concatenated with the `+` operator. – interjay Oct 26 '15 at 09:27
  • Possible duplicate of [Modify const char \* in C](http://stackoverflow.com/questions/33263493/modify-const-char-in-c) – ams Oct 26 '15 at 09:33
  • Your recursion is one that works nicely for true string objects, but not for `char*`. You need to use a different recursion, namely in-place swapping of characters. – MicroVirus Oct 26 '15 at 09:39
  • This is not a duplicate of [Modify const char*](http://stackoverflow.com/questions/33263493/modify-const-char-in-c) since that uses a void function. Unless I'm reading the problem wrong, we're not allowed to use anything outside of the recursive function's scope – FyrusDak Oct 26 '15 at 09:54
  • Recursive solutions that are only using this function (i.e. no second helper function) are bound to be complicated, slow and inefficient. Maybe you're interpreting the requirements too strictly. – Heinzi Oct 26 '15 at 09:58

5 Answers5

2

You cannot use the plus operator to concatenate char* pointers.

If you're allowed to, I'd rather take an inplace approach. Don't want to say too much here, but you can reverse a string without accessing any memory outside of the string. Your recursion could then pass pointers around.

You'll probably need a helper function

void reverse(char *start, char *end)

you're calling from your main algorithm.

edit: It would be a recursive solution, but the recursive call would be to the helper function rather than to the main function. Don't know whether that's allowed in your assignment. There might also be a way of making it recurse on the main function, but that'd be more complicated.

Heinzi
  • 5,793
  • 4
  • 40
  • 69
  • 1
    Helper is not necessary, if you wish to give up on tail calls. Remeber the last char in the string, zero it, recursive call, then restore it back. Man, I can't stop giggling at the image of the stack at the deepest point :) – Joker_vD Oct 26 '15 at 10:04
0

You need to remember the last character and then reverse the string without the shorter character.

So

F("yourstring") = "g" + F("yourstrin") 

etc etc

Which fortunately can be done in place with careful use of memcpy

Tom Tanner
  • 9,244
  • 3
  • 33
  • 61
0

I think this code will work. Sorry for writing such a basic one. I am just learning C++.

#include<iostream.h>
#include<string.h>
#include<conio.h>
char *rev(char *s,int n,int i)
{
char t;
if(i<n/2)
{
t=s[i];
s[i]=s[n-i-1];
s[n-i-1]=t;
i++;
rev(s,n,i);
}
return s;
}
void main()
{
char *s;
cout<<"\n\tEnter a String ";
cin>>s;
int n=strlen(s);
rev(s,n,0);
cout<<"\n\tReversed String :"<<s;
getch();
}
  • 1
    Please format your code better. Also, consider rewriting your answer in standard C++ (`` instead of ``, `` instead of ``, no `` at all), etc. – isekaijin Feb 19 '17 at 18:08
  • My turboc3 compiler is not able to find , @pyon – Ayosh Maitra Feb 19 '17 at 18:12
  • Normally I don't suggest people to ditch old technologies just because they're old. But in this particular case, I'm going to: Please switch to a modern, standard-compliant C++ implementation, like g++ or clang. As for ``, it's DOS and Windows-specific, so it won't work on other systems. – isekaijin Feb 19 '17 at 18:15
  • Sir, what about the logic of this code ? Is this good , or too basic ? @pyon – Ayosh Maitra Feb 19 '17 at 18:24
  • There's nothing wrong with the underlying algorithm, that's why I didn't comment on that. But I would have written it like [this](http://ideone.com/ozz1NL). It's the same algorithm, but written in more idiomatic C++. – isekaijin Feb 19 '17 at 18:38
  • @pyon Sir, can you please follow this link and check the code i have written?http://stackoverflow.com/questions/1995328/are-there-any-better-methods-to-do-permutation-of-string/42348015#42348015 – Ayosh Maitra Feb 20 '17 at 15:11
-1

If it's recursive function, it should handle input like this:

  1. F( "yourstring" )
  2. F( "ourstring" )
  3. F( "urstring" )

It prompts to handle one char each time. pseudo-code should like

char* F( char* input)
{
    move last char to the first;
    F(input+1);
    return input;
}

Working code:

char* reverse( char* input )
{
    int len = strlen( input );
    char tmp = input[ len - 1 ];
    if( len > 1 )
    {
        for( int i = len - 1; i > 0; i-- )
        {
            input[ i ] = input[ i - 1 ];
        }
        input[ 0 ] = tmp;
        reverse( input + 1 );
    }
    return input;
}
qxg
  • 6,955
  • 1
  • 28
  • 36
-1
char* reverse(char *str)
{
    static int i=0;
    static char revstr[100] = "";
   if (*str)
   {
       reverse(str+1);
       revstr[i++]=*str;
   } 
   return revstr;
}
Grv
  • 273
  • 3
  • 14
  • What if you reversed two strings and then use the first one again? This is not a good solution. – aslg Oct 26 '15 at 09:41
  • The function is calling itself this is how it is reversing the string. And to my knowledge function calling itself is recursive. if i am wrong pls through some light – Grv Oct 26 '15 at 09:48