문제
strncat 라이브러리 함수를 사용하여 소스 문자열에서 대상 문자열로 n개의 문자를 연결하는 C 프로그램 작성
해결책
strcat 함수
-
이 함수는 두 문자열을 결합하거나 연결하는 데 사용됩니다.
-
대상 문자열의 길이는 원본 문자열보다 커야 합니다.
-
결과적으로 연결된 문자열은 소스 문자열에 있습니다.
구문
strcat (Destination String, Source string);
예시 1
#include <string.h>
main(){
char a[50] = "Hello";
char b[20] = "Good Morning";
clrscr ( );
strcat (a,b);
printf("concatenated string = %s", a);
getch ( );
} 출력
Concatenated string = Hello Good Morning
strncat 함수
-
이 함수는 한 문자열의 n개 문자를 다른 문자열로 결합하거나 연결하는 데 사용됩니다.
-
대상 문자열의 길이는 원본 문자열보다 커야 합니다.
-
결과적으로 연결된 문자열은 대상 문자열에 있습니다.
구문
strncat (Destination String, Source string,n);
예시 2
#include <string.h>
main ( ){
char a [30] = "Hello";
char b [20] = "Good Morning";
clrscr ( );
strncat (a,b,4);
a [9] = ‘\0’;
printf("concatenated string = %s", a);
getch ( );
} 출력
Concatenated string = Hello Good.
예시 3
#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 :TutorialPoint Enter the destination string before :C Programming The modified destination string :C Tur