Mega Code Archive

 
Categories / C / Function
 

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 */ }