C 라이브러리 함수 char *strcpy(char *dest, const char *src) src가 가리키는 문자열을 복사합니다. 목적지로 .
문자 배열을 문자열이라고 합니다.
선언
다음은 배열에 대한 선언입니다.
char stringname [size];
예를 들어 - char string[50]; 길이 50자의 문자열
초기화
- 단일 문자 상수 사용 -
char string[10] = { ‘H’, ‘e’, ‘l’, ‘l’, ‘o’ ,‘\0’} - 문자열 상수 사용하기 -
char string[10] = "Hello":;
액세스 − '\0'이 나타날 때까지 문자열에 액세스하는 데 사용되는 제어 문자열 "%s"가 있습니다.
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