简体   繁体   中英

C - (recursively) given a string, return its reverse

I'm pretty sure that its a noob question, but i can't find the solution to the problem. I'm trying to make a recursive function that takes an string 's' and returns that string but inverted. I figured out that i could do that with two functions, one that copies the characters from the string to an auxiliary, and other that copies from the auxiliary to the original string again (but this time, inverted). But i want to make a function that does all of that. this is how i made the function, but it doesn't work:

/* external variables */

#define TRUE 1
#define FALSE 0

int e = 0;
i = 0;
int ret = FALSE;
char Saux[255];

void Inverse(char s[], int i) {
    if (s[i] == '\n' || ret == TRUE) {
        if (ret == FALSE) {
            ret == TRUE;
            i -= 1;
        }    
        if (i == 0) {
            s[e] = Saux[i];
            return;
        } else {
            s[e++] = Saux[i];
            return Inverse(s, i - 1);
        }
    } else /* this will happen until s[ i ] == '\n' */
    {
        Saux[i] = s[i];
        return Inverse(s, i + 1);
    }
}

I'm learning C, and I'm not too good with recursive functions, so if there is a better way to make this function please let me know. also, English isn't my native language so sorry for any misspelling. thank you.

Your problem is here:

ret == TRUE; // should be ret = TRUE;

Maybe you can think of your problem in a different way: instead of going twice through the string, start at both the beginning ( i ) and the end ( j ) of the string. Swap the characters at i and j and increment the counters (or decrement respectively) until you reach the middle.

like so (here end means one past the last element which is initially the string length):

#include <string.h>

void swap(char* str, size_t i, size_t j) {
    char tmp = str[i];
    str[i] = str[j];
    str[j] = tmp;
}

void reverse(char* str, size_t begin, size_t end) {
    if(begin + 1 >= end)
        return;
    swap(str, begin, end - 1);
    reverse(str, begin + 1, end - 1);
}

int main() {
    char str[] = "foobar";
    reverse(str, 0, strlen(str));
    return 0;
}

The trick is in making these things recursive and then combining it with our ability to use the pointer. When we have access to a length parameter we can solve this by moving our pointer forward one and reduce our count by two on each step.

void reverseStringUsingLength(char* inputString, int len) {
  if(len < 2) return;
  char t = inputString[0];
  inputString[0] = inputString[len-1];
  inputString[len-1] = t;
  reverseStringUsingLength(&inputString[1], len-2);
}

here is a simple code to reverse string

#include <stdio.h>
#include <time.h>


char test[] = "HELLO";

void inverse(char s[], int start, int end){

    if(end > start){
        char temp = s[start];
        s[start] = s[end];
        s[end] = temp;
        inverse(s, start+1, end-1);
    }

}

int main(int argc, const char * argv[])
{

    inverse(test, 0, strlen(test)-1);
    printf("hello = %s\n", test);  

    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM