Solved programming problem

Copy One string to another using Recursion in C.

Source code

#include<stdio.h>

void copy_str(char str[],char new_str[],int i)

{

    if(str[i]=='') ///base case

    {

        new_str[i]='';

        return;

    }

    new_str[i] = str[i]; ///copy string

    copy_str(str,new_str,i+1); ///call the function by adding 1 with i

}

int main()

{

    char str[100],new_str[100];

    printf("Please Enter A String: ");

    gets(str);

    copy_str(str,new_str,0);

    printf("Your Copied String: ");

    puts(new_str);

    printf("
");

    return 0;

}

Output

Please Enter A String: code to haven
Your Copied String: code to haven

 

Join the discussion

Add your comment