Function to copy string (Iterative and Recursive)
Last Updated :
07 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:
C++
#include <bits/stdc++.h>
using namespace std;
void myCopy( char s1[], char s2[])
{
int i = 0;
for (i=0; s1[i] != '\0' ; i++)
s2[i] = s1[i];
s2[i] = '\0' ;
}
int main()
{
char s1[100] = "GEEKSFORGEEKS" ;
char s2[100] = "" ;
myCopy(s1, s2);
cout << s2;
return 0;
}
|
Java
class GFG
{
static void myCopy( char s1[], char s2[])
{
int i = 0 ;
for (i = 0 ; i < s1.length; i++)
s2[i] = s1[i];
}
public static void main(String[] args)
{
char s1[] = "GEEKSFORGEEKS" .toCharArray();
char s2[] = new char [s1.length];
myCopy(s1, s2);
System.out.println(String.valueOf(s2));
}
}
|
Python3
def myCopy(s1,s2):
for i in range ( len (s1)):
s2[i] = s1[i]
return "".join(s2)
s1 = list ( "GEEKSFORGEEKS" )
s2 = [""] * len (s1)
print (myCopy(s1,s2))
|
C#
using System;
class GFG
{
static void myCopy( char []s1, char []s2)
{
int i = 0;
for (i = 0; i < s1.Length; i++)
s2[i] = s1[i];
}
public static void Main(String[] args)
{
char []s1 = "GEEKSFORGEEKS" .ToCharArray();
char []s2 = new char [s1.Length];
myCopy(s1, s2);
Console.WriteLine(String.Join( "" , s2));
}
}
|
Javascript
<script>
function myCopy(s1, s2)
{
let i = 0;
for (i = 0; i < s1.length; i++)
s2[i] = s1[i];
}
let s1 = "GEEKSFORGEEKS" ;
let s2 = [];
let index = 0;
myCopy(s1, s2, index);
document.write(s2.join( "" ));
</script>
|
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:
C++
#include <bits/stdc++.h>
using namespace std;
void myCopy( char s1[], char s2[], int index = 0)
{
s2[index] = s1[index];
if (s1[index] == '\0' )
return ;
myCopy(s1, s2, index + 1);
}
int main()
{
char s1[100] = "GEEKSFORGEEKS" ;
char s2[100] = "" ;
myCopy(s1, s2);
cout << s2;
return 0;
}
|
Java
class GFG
{
static void myCopy( char s1[],
char s2[], int index)
{
s2[index] = s1[index];
if (index == s1.length - 1 )
{
return ;
}
myCopy(s1, s2, index + 1 );
}
public static void main(String[] args)
{
char s1[] = "GEEKSFORGEEKS" .toCharArray();
char s2[] = new char [s1.length];
int index = 0 ;
myCopy(s1, s2, index);
System.out.println(String.valueOf(s2));
}
}
|
Python3
def copy_str(x, y):
if len (y) = = 0 :
return x
else :
c = copy_str(x, (y)[ 1 : - 1 ])
return c
x = input ( "hello" )
y = input ( "no" )
print (copy_str(x, y))
|
C#
using System;
class GFG
{
static void myCopy( char []s1,
char []s2, int index)
{
s2[index] = s1[index];
if (index == s1.Length - 1)
{
return ;
}
myCopy(s1, s2, index + 1);
}
public static void Main(String[] args)
{
char []s1 = "GEEKSFORGEEKS" .ToCharArray();
char []s2 = new char [s1.Length];
int index = 0;
myCopy(s1, s2, index);
Console.WriteLine(String.Join( "" , s2));
}
}
|
Javascript
<script>
function myCopy(s1, s2, index)
{
s2[index] = s1[index];
if (index == s1.length - 1)
{
return ;
}
myCopy(s1, s2, index + 1);
}
var s1 = "GEEKSFORGEEKS" ;
var s2 = [];
var index = 0;
myCopy(s1, s2, index);
document.write(s2.join( "" ));
</script>
|
Time Complexity: O(m), Here m is the length of string s1.
Auxiliary Space: O(m), due to recursive call stack
Share your thoughts in the comments
Please Login to comment...