VOOZH about

URL: https://www.geeksforgeeks.org/dsa/function-copy-string-iterative-recursive/

⇱ Function to copy string (Iterative and Recursive) - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Function to copy string (Iterative and Recursive)

Last Updated : 7 Dec, 2022

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:


Output
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:


Output: 
GEEKSFORGEEKS

 

Time Complexity: O(m), Here m is the length of string s1.
Auxiliary Space: O(m), due to recursive call stack

Comment
Article Tags: