Copy string using recursion : Function Recursive « Function « C / ANSI-C






Copy string using recursion

  
#include <stdio.h>

void strcopy(char *s1, char *s2);

int main(void)
{
  char str[80];

  strcopy(str, "this is a test");
  printf(str);

  return 0;
}


void strcopy(char *s1, char *s2)
{
  if(*s2) { /* if not at end of s2 */
    *s1++ = *s2++;
    strcopy(s1, s2);
  }
  else *s1 = '\0'; /* null terminate the string */
}


           
       








Related examples in the same category

1.A recursive power functionA recursive power function
2.Calculating factorials using recursion
3.Prints out Fibonacci numbersPrints out Fibonacci numbers
4.Recursive function call
5.Function: Recursive call
6.Recursive function with static variable