Quick Tip: Print The Reverse of a Null Terminated String Using Recursion In C

Posted July 18, 2009 @ 11:07pm by Phil

After reading my previous quick tip on reversing a null terminated string using recursion, another one of my friends sent me a link to a very elegant function that prints the reverse of a string using recursion:

void print_reverse (char *str)
{
  if (*str)
  {
    print_reverse(str + 1);
    putchar(*str);
  }
}

Download Source: print_reverse.c

Tags: C/C++, Interview Questions, Quick Tip