C 라이브러리 함수 char *strncat(char *dest, const char *src, size_t n) src가 가리키는 문자열을 최대 n자 길이의 dest가 가리키는 문자열 끝에 추가합니다.
문자 배열을 문자열이라고 합니다.
선언
다음은 배열에 대한 선언입니다 -
char stringname [size];
예:char string[50]; 길이 50자의 문자열
초기화
- 단일 문자 상수 사용 -
char string[10] = { ‘H’, ‘e’, ‘l’, ‘l’, ‘o’ ,‘\0’} - 문자열 상수 사용하기 -
char string[10] = "Hello":;
액세스 − '\0'이 나타날 때까지 문자열에 액세스하는 데 사용되는 제어 문자열 "%s"가 있습니다.
strncat() 함수
-
이것은 한 문자열의 n개 문자를 다른 문자열로 결합하거나 연결하는 데 사용됩니다.
-
대상 문자열의 길이가 원본 문자열보다 깁니다.
-
결과 연결된 문자열은 소스 문자열에 있습니다.
구문은 다음과 같습니다 -
strncat (Destination String, Source string,n);
예시
다음 프로그램은 strncat() 함수의 사용법을 보여줍니다 -
#include <string.h>
main ( ){
char a [30] = "Hello \n";
char b [20] = "Good Morning \n";
strncat (a,b,4);
a [9] = "\0";
printf("concatenated string = %s", a);
} 출력
위의 프로그램이 실행되면 다음과 같은 결과가 생성됩니다 -
Concatenated string = Hello Good.
다른 예를 봅시다 -
다음은 strncat 라이브러리 함수 -
를 사용하여 소스 문자열에서 대상 문자열로 n개의 문자를 연결하는 C 프로그램입니다.#include<stdio.h>
#include<string.h>
void main(){
//Declaring source and destination strings//
char source[45],destination[50];
//Reading source string and destination string from user//
printf("Enter the source string :");
gets(source);
printf("Enter the destination string before :");
gets(destination);
//Concatenate all the above results//
destination[2]='\0';
strncat(destination,source,2);
strncat(destination,&source[4],1);
//Printing destination string//
printf("The modified destination string :");
puts(destination);
} 출력
위의 프로그램이 실행되면 다음과 같은 결과가 생성됩니다 -
Enter the source string :Tutorials Point Enter the destination string before :Tutorials Point C Programming The modified destination string :TuTur