![]() |
VOOZH | about |
Given two strings, copy one string to another using recursion. We basically need to write our own recursive version of strcpy in C/C++
Examples:
Input : s1 = "hello" s2 = "geeksforgeeks" Output : s2 = "hello" Input : s1 = "geeksforgeeks" s2 = "" Output : s2 = "geeksforgeeks"
Iterative: Copy every character from s1 to s2 starting from index = 0 and in each call increase the index by 1 until s1 doesn't reach to end;
Implementation:
GEEKSFORGEEKS
Time Complexity: O(m), Here m is the length of string s1.
Auxiliary Space: O(1), As constant extra space is used.
Recursive: Copy every character from s1 to s2 starting from index = 0 and in each call increase the index by 1 until s1 doesn't reach to end;
Implementation:
GEEKSFORGEEKS
Time Complexity: O(m), Here m is the length of string s1.
Auxiliary Space: O(m), due to recursive call stack