Write a recursive function to reverse a string in c? Time Complexity: O(n).

write a c program to reverse a string, for example if you are given a string “wikistack” your output should be “kcatsikiw”. http://wikistack.com/three-laws-recursion/

A recursive algorithms must satisfy below three things.

(1) A recursive algorithm must have a base case.
(2) A recursive algorithm must change its state and move toward the base case.
(3) A recursive algorithm must call itself.

/* using recursion */

#include<stdio.h>

void print (char *str);

int main ()
{

  char *str = "wikistack";

  // call print function
  print (str);

  printf ("\n");   // for new line
  return 0;
}

void print (char *str)
{
  if (*str)
      print (str + 1);

  printf ("%c", *str);
}



Related Contents to follow