c语言中字符串的复制


c语言中字符串的复制。

1、自定义函数

#include <stdio.h>

char *str_copy(char *d, char *s)
{
    char *t = d;
    
    while(*d++ = *s++)
        ;
    return t;
}

int main(void)
{
    char str[128] = "ABCDEFG";
    char tmp[128];
    
    printf("str: %s\n", str);
    
    printf("tmp: "); scanf("%s", tmp);
    
    printf("str:  %s\n", str_copy(str, tmp));
    
    return 0;
}

 

 

2、strcpy函数

#include <stdio.h>
#include <string.h>  // strcpy函数的头文件。 

int main(void)
{
    char str[128] = "abcde";
    char tmp[128];
    
    printf("str: %s\n", str);
    
    printf("tmp: "); scanf("%s", tmp);
    
    printf("str: %s\n", strcpy(str, tmp));
    
    return 0;    
} 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM