📜  复制字符串的函数(迭代和递归)

📅  最后修改于: 2021-04-26 09:14:23             🧑  作者: Mango

给定两个字符串,请使用递归将一个字符串复制到另一个字符串。我们基本上需要用C / C++编写我们自己的strcpy的递归版本
例子:

Input : s1 = "hello"
        s2 = "geeksforgeeks"
Output : s2 = "hello"

Input :  s1 = "geeksforgeeks"
         s2 = ""
Output : s2 = "geeksforgeeks"

迭代式:
从index = 0开始将每个字符从s1复制到s2,并在每个调用中将索引增加1,直到s1没有结束。

C++
// Iterative CPP Program to copy one String 
// to another.
#include 
using namespace std;
 
// Function to copy one string to other
// assuming that other string has enough
// space.
void myCopy(char s1[], char s2[])
{
    int i = 0;
    for (i=0; s1[i] != '\0'; i++)
       s2[i] = s1[i];
    s2[i] = '\0';
}
 
// Driver function
int main()
{
    char s1[100] = "GEEKSFORGEEKS";
    char s2[100] = "";
    myCopy(s1, s2);
    cout << s2;
    return 0;
}


Java
// Iterative Java Program to copy one String
// to another.
class GFG
{
     
// Function to copy one string to other
// assuming that other string has enough
// space.
static void myCopy(char s1[], char s2[])
{
    int i = 0;
    for (i = 0; i < s1.length; i++)
         s2[i] = s1[i];
}
 
// Driver code
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));
}
}
 
// This code contributed by Rajput-Ji


Python3
# recursive Python Program to copy one String
# to another.
 
# Function to copy one string to other
def copy_str(x,y):
    if len(y)==0:
        return x
    else:
        c = copy_str(x,(y)[1:-1])
        return c
x = input()
y = input()
print(copy_str(x,y))
 
# This code contributed by deeksha20049@iiid.ac.in
#deeksha20049


C#
// Iterative C# Program to copy one String
// to another.
using System;
 
class GFG
{
     
// Function to copy one string to other
// assuming that other string has enough
// space.
static void myCopy(char []s1, char []s2)
{
    int i = 0;
    for (i = 0; i < s1.Length; i++)
        s2[i] = s1[i];
}
 
// Driver code
public static void Main(String[] args)
{
    char []s1 = "GEEKSFORGEEKS".ToCharArray();
    char []s2 = new char[s1.Length];
    myCopy(s1, s2);
    Console.WriteLine(String.Join("", s2));
}
}
 
// This code is contributed by 29AjayKumar


C++
// CPP Program to copy one String to
// another using Recursion
#include 
using namespace std;
 
// Function to copy one string in to other
// using recursion
void myCopy(char s1[], char s2[], int index = 0)
{
    // copying each character from s1 to s2
    s2[index] = s1[index];
 
    // if string reach to end then stop
    if (s1[index] == '\0') 
        return;
 
    // increase character index by one
    myCopy(s1, s2, index + 1);
}
 
// Driver function
int main()
{
    char s1[100] = "GEEKSFORGEEKS";
    char s2[100] = "";
    myCopy(s1, s2);
    cout << s2;
    return 0;
}


Java
// Java Program to copy one String to
// another using Recursion
class GFG
{
     
    // Function to copy one string in to other
    // using recursion
    static void myCopy(char s1[],
                       char s2[], int index)
    {
        // copying each character from s1 to s2
        s2[index] = s1[index];
 
        // if string reach to end then stop
        if (index == s1.length - 1)
        {
            return;
        }
 
        // increase character index by one
        myCopy(s1, s2, index + 1);
    }
 
    // Driver Code
    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));
    }
}
 
// This code is contributed by PrinciRaj1992


Python3
# recursive Python Program to copy one String
# to another.
 
# Function to copy one string to other
 
 
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))
 
# This code contributed by deeksha20049@iiid.ac.in


C#
// C# Program to copy one String to
// another using Recursion
using System;
 
class GFG
{
     
    // Function to copy one string in to other
    // using recursion
    static void myCopy(char []s1,
                       char []s2, int index)
    {
        // copying each character from s1 to s2
        s2[index] = s1[index];
 
        // if string reach to end then stop
        if (index == s1.Length - 1)
        {
            return;
        }
 
        // increase character index by one
        myCopy(s1, s2, index + 1);
    }
 
    // Driver Code
    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));
    }
}
 
// This code is contributed by Princi Singh


输出:
GEEKSFORGEEKS

递归的:
从index = 0开始将每个字符从s1复制到s2,并在每个调用中将索引增加1,直到s1没有结束。

C++

// CPP Program to copy one String to
// another using Recursion
#include 
using namespace std;
 
// Function to copy one string in to other
// using recursion
void myCopy(char s1[], char s2[], int index = 0)
{
    // copying each character from s1 to s2
    s2[index] = s1[index];
 
    // if string reach to end then stop
    if (s1[index] == '\0') 
        return;
 
    // increase character index by one
    myCopy(s1, s2, index + 1);
}
 
// Driver function
int main()
{
    char s1[100] = "GEEKSFORGEEKS";
    char s2[100] = "";
    myCopy(s1, s2);
    cout << s2;
    return 0;
}

Java

// Java Program to copy one String to
// another using Recursion
class GFG
{
     
    // Function to copy one string in to other
    // using recursion
    static void myCopy(char s1[],
                       char s2[], int index)
    {
        // copying each character from s1 to s2
        s2[index] = s1[index];
 
        // if string reach to end then stop
        if (index == s1.length - 1)
        {
            return;
        }
 
        // increase character index by one
        myCopy(s1, s2, index + 1);
    }
 
    // Driver Code
    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));
    }
}
 
// This code is contributed by PrinciRaj1992

Python3

# recursive Python Program to copy one String
# to another.
 
# Function to copy one string to other
 
 
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))
 
# This code contributed by deeksha20049@iiid.ac.in

C#

// C# Program to copy one String to
// another using Recursion
using System;
 
class GFG
{
     
    // Function to copy one string in to other
    // using recursion
    static void myCopy(char []s1,
                       char []s2, int index)
    {
        // copying each character from s1 to s2
        s2[index] = s1[index];
 
        // if string reach to end then stop
        if (index == s1.Length - 1)
        {
            return;
        }
 
        // increase character index by one
        myCopy(s1, s2, index + 1);
    }
 
    // Driver Code
    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));
    }
}
 
// This code is contributed by Princi Singh
输出:
GEEKSFORGEEKS