Signup/Sign In

C Program to Copy One String to Another using Recursion

learn c Language

Logic To Copy A String To Another String Using Recursion:

  • By using the user-defined function void recur (char[],char[],int);
  • The function is used to copy one string to another string using the recursion function
  • For this program, we use two strings (str & str1), The string str is used to read the input from the user and store it,
  • Another string (str1) is used to copy the string recursively.
  • Here the index value is incremented by 1 to move the recursion state.
  • The String in str is copied to str1

C Program To Copy a String To Another String Using Recursion:

#include <stdio.h>
 
void recur(char [], char [], int);
 
int main()
{
    char str[30], str1[30];
 
    printf("Enter The String: ");
    scanf("%[^\n]s", str);
    recur(str, str1, 0);
    printf("Executed Successfully\n");
    printf("The input String: %s\n", str);
    printf("The Copied String: %s\n", str1);
    return 0;
}
 
void recur(char str[], char str1[], int index)
{
    str1[index] = str[index];
    if (str[index] == '\0')
        return;
    recur(str, str1, index + 1);
}

Output:

Recursion