C Exercises: Copy One string to another
C Recursion : Exercise-19 with Solution
Write a program in C to copy one string to another using recursion.
Pictorial Presentation:

Sample Solution:
C Code:
#include <stdio.h>
void copyString(char [], char [], int);
int main()
{
char stng1[20], stng2[20];
printf("\n\n Recursion : Copy One string to another :\n");
printf("---------------------------------------------\n");
printf(" Input the string to copy : ");
scanf("%s", stng1);
copyString(stng1, stng2, 0);
printf("\n The string successfully copied.\n\n");
printf(" The first string is : %s\n", stng1);
printf(" The copied string is : %s\n\n", stng2);
return 0;
}
void copyString(char stng1[], char stng2[], int ctr)
{
stng2[ctr] = stng1[ctr];
if (stng1[ctr] == '\0')
return;
copyString(stng1, stng2, ctr + 1);
}
Sample Output:
Recursion : Copy One string to another : --------------------------------------------- Input the string to copy : w3resource The string successfully copied. The first string is : w3resource The copied string is : w3resource
Explanation:
void copyString(char stng1[], char stng2[], int ctr) { stng2[ctr] = stng1[ctr]; if (stng1[ctr] == '\0') return; copyString(stng1, stng2, ctr + 1); }
The copyString() function takes two character arrays stng1 and stng2 and an integer ctr as arguments. It recursively copies the characters of stng1 to stng2 starting from index ctr until it finds a null character (\0) in stng1. At that point, it returns from the function.
- The character at index ctr of stng1 is copied to the corresponding index of stng2.
- If the character at index ctr of stng1 is null (\0), the function returns.
- Otherwise, the function calls itself with ctr+1 as the new index.
- The function continues to copy the characters until it reaches the end of stng1.
Time complexity and space complexity:
The time complexity of this function is O(n) where n is the length of the input string stng1.
The space complexity is O(n) as well since the function uses recursion to copy the string, which creates a new stack frame for each recursive call.
Flowchart:

C Programming Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a program in C to find the Hailstone Sequence of a given number upto 1.
Next: Write a program in C to find the first capital letter in a string using recursion.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
C Programming: Tips of the Day
__FILE__ macro shows full path
#include <string.h> #define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__) For Windows use '\\' instead of '/'.
Ref : https://bit.ly/3iEWRoT
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook