📜  在C / C++中复制字符串的不同方法

📅  最后修改于: 2021-05-30 03:27:32             🧑  作者: Mango

使用内置函数

使用字符串.h头文件中的内置函数strcpy()将一个字符串复制到另一个字符串。 strcpy()接受指向目标数组和源数组的指针作为参数,复制后返回指向目标字符串的指针。用%s就可以打印字符串(%s打印从基地地址,直到空字符的字符串)。

下面是使用上述方法的实现:

C
// C program to copy the string using
// strcpy function
  
#include 
#include 
#include 
  
// Function to copy the string
char* copyString(char s[])
{
    char* s2;
    s2 = (char*)malloc(20);
  
    strcpy(s2, s);
    return (char*)s2;
}
  
// Driver Code
int main()
{
    char s1[20] = "GeeksforGeeks";
    char* s2;
  
    // Function Call
    s2 = copyString(s1);
    printf("%s", s2);
    return 0;
}


C
// C program to copy string using loops
  
#include 
#include 
#include 
  
// Function to copy the string
char* copyString(char s[])
{
    int i;
    char* s2;
    s2 = (char*)malloc(20);
  
    // Executing till null character
    // is found
    for (i = 0; s[i] != '\0'; i++) {
  
        // Copy the character one
        // by one from s1 to s2
        s2[i] = s[i];
    }
  
    // Return the pointer of newly
    // created string
    return (char*)s2;
}
  
// Driver Code
int main()
{
    char s1[20] = "GeeksforGeeks";
    char* s2;
  
    // Function Call
    s2 = copyString(s1);
    printf("%s", s2);
    return 0;
}


C
// C program to copy the string
// using pointers
  
#include 
#include 
#include 
  
// Function to copy the string
char* copyString(char s[])
{
  
    char *s2, *p1, *p2;
  
    s2 = (char*)malloc(20);
    p1 = s;
    p2 = s2;
  
    // Executing till the null
    // character is found
    while (*p1 != '\0') {
  
        // Copy the content of s1 to s2
        *p2 = *p1;
        p1++;
        p2++;
    }
    *p2 = '\0';
  
    return s2;
}
  
// Driver Code
int main()
{
    char s1[20] = "GeeksforGeeks";
    char* s2;
  
    s2 = copyString(s1);
    printf("%s", s2);
    return 0;
}


C
// C program to copy the string
  
#include 
#include 
  
// Function to copy the string
void copyString(char* t, char* s)
{
    // (return ASCII value which is True,
    // therefore will be in the loop
    // till the condition is False
    while (*t++ = *s++)
        ;
}
  
// Driver Code
int main()
{
    char s2[20] = "GeeksforGeeks";
    char s1[20];
  
    // Function Call
    copyString(s1, s2);
    printf("%s", s1);
    return 0;
}


输出:
GeeksforGeeks

使用循环

这个想法是使用for循环将第一个数组的内容一个一个地复制到第二个数组。

下面是使用上述方法的实现:

C

// C program to copy string using loops
  
#include 
#include 
#include 
  
// Function to copy the string
char* copyString(char s[])
{
    int i;
    char* s2;
    s2 = (char*)malloc(20);
  
    // Executing till null character
    // is found
    for (i = 0; s[i] != '\0'; i++) {
  
        // Copy the character one
        // by one from s1 to s2
        s2[i] = s[i];
    }
  
    // Return the pointer of newly
    // created string
    return (char*)s2;
}
  
// Driver Code
int main()
{
    char s1[20] = "GeeksforGeeks";
    char* s2;
  
    // Function Call
    s2 = copyString(s1);
    printf("%s", s2);
    return 0;
}
输出:
GeeksforGeeks

使用指针

这个想法是使用指针将字符串数组的内容复制到另一个数组,并通过遍历新的指针来打印结果字符串。

下面是使用上述方法的实现:

C

// C program to copy the string
// using pointers
  
#include 
#include 
#include 
  
// Function to copy the string
char* copyString(char s[])
{
  
    char *s2, *p1, *p2;
  
    s2 = (char*)malloc(20);
    p1 = s;
    p2 = s2;
  
    // Executing till the null
    // character is found
    while (*p1 != '\0') {
  
        // Copy the content of s1 to s2
        *p2 = *p1;
        p1++;
        p2++;
    }
    *p2 = '\0';
  
    return s2;
}
  
// Driver Code
int main()
{
    char s1[20] = "GeeksforGeeks";
    char* s2;
  
    s2 = copyString(s1);
    printf("%s", s2);
    return 0;
}
输出:
GeeksforGeeks

使用指针和后递增

这个想法是使用while循环将字符串array1的内容一一分配给字符串array2,并使用后运算符递增,因为它返回ASCII值,因此条件为true,它将在循环中传输直到条件为错误,我们将脱颖而出。

下面是使用上述方法的实现:

C

// C program to copy the string
  
#include 
#include 
  
// Function to copy the string
void copyString(char* t, char* s)
{
    // (return ASCII value which is True,
    // therefore will be in the loop
    // till the condition is False
    while (*t++ = *s++)
        ;
}
  
// Driver Code
int main()
{
    char s2[20] = "GeeksforGeeks";
    char s1[20];
  
    // Function Call
    copyString(s1, s2);
    printf("%s", s1);
    return 0;
}
输出:
GeeksforGeeks

注意:在上述所有方法中,目标数组的大小必须大于源字符串的长度才能复制所有字符。

要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”