0

I want to create a function that reverses a C-string. It does not return the C-string to me. But the address of sortante is indeed the beginning of my chain terminated by a terminal 0.

#include <stdio.h>
#include <stdlib.h>

const char txt[] = "salut";

char *inverser(const char *entrante);
int main()
{
    puts(inverser(txt));
    return 0;
}

char *inverser(const char *entrante)
{
    static char sortante[9];
    const char *i; char *o;

    i = entrante; o = sortante;

    while (*i++);
    i--;
    while (i >= entrante)
        *o++ = *i--;
    *o = '\0';

    return sortante;
}

When I run this code on codeblocks I get nothing as output. I think I can reverse my C-string "salut", which should have displayed "tulas".

cafce25
  • 15,907
  • 4
  • 25
  • 31
  • 1
    Try changing `while (*i++);` to `while (*i) i++;` When the condition is false (at the '\0'), `i` should not be incremented another time.. – Fe2O3 Mar 26 '23 at 07:56

0 Answers0