C语言中的strcpy()函数是什么?
C 库函数 char *strcpy(char *dest, const char *src) 将 src 指向的字符串复制到 dest 中。
字符数组称为字符串。
声明
以下是数组的声明
char stringname [size];
例如 − char string[50]; 字符串的长度为 50 个字符
初始化
- 使用单个字符常量 −
char string[10] = { ‘H’, ‘e’, ‘l’, ‘l’, ‘o’ ,‘\0’}- 使用字符串常量 −
char string[10] = "Hello":;
访问 − 有一个控制字符串 "%s" 用于访问字符串,直到它遇到‘\0’
strcpy () 函数
此函数用于将源字符串复制到目标字符串中。
目标字符串的长度大于或等于源字符串的长度。
语法
语法如下 −
strcpy (Destination string, Source String);
示例
以下示例显示了 strcpy() 函数的用法。
char a[50]; char a[50];
strcpy ("Hello",a); strcpy ( a,"hello");
output: error output: a= "Hello"程序
以下程序显示了 strcpy() 函数的用法。
#include <string.h>
main ( ){
char a[50], b[50];
printf ("enter a source string");
scanf("%s", a);
strcpy ( b,a);
printf ("copied string = %s",b);
getch ( );
}输出
执行上述程序后,它会产生以下结果 −
Enter a source string : Hello Copied string = Hello

让我们来看另一个有关 strcpy 的示例。
以下是演示 strcpy 库函数的 C 程序 −
程序
#include<stdio.h>
#include<string.h>
void main(){
//Declaring source and destination strings//
char source[25],destination[50];
//Reading Input from user//
printf("Enter the string to be copied : ");
gets(source);
printf("Enter the existing destination string : ");
gets(destination);
//Using strcpy library function//
strcpy(destination,source);
//Printing destination string//
printf("Destination string is : ");
puts(destination);
}输出
执行上述程序后,它会产生以下结果 −
Enter the string to be copied : C programming Enter the existing destination string : bhanu Destination string is : C programming
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C编程
C++
C#
MongoDB
MySQL
Javascript
PHP