42.6 연습문제: 문자열 포인터를 배열에 복사하기
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
int main(void)
{
char* s1 = "C language";
char s2[20];
strcpy(s2, s1);
printf("%s\n", s2);
return 0;
}
42.7 연습문제: 문자열 포인터를 동적 메모리에 복사하기
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
char* s1 = "The Little Prince";
char* s2 = malloc(sizeof(char) * 20);
strcpy(s2, s1);
printf("%s\n", s2);
free(s2);
return 0;
}
42.8 연습문제: 문자 배열을 붙이기
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
int main(void)
{
char s1[20] = " 교향곡 9번";
char s2[40] = "베토벤";
strcat(s2, s1);
printf("%s\n", s2);
return 0;
}
42.9 연습문제: 문자열 리터럴과 동적 메모리 붙이기
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
char* s1 = " Wonderland";
char* s2 = malloc(sizeof(char) * 20);
strcpy(s2, "Alice in");
strcat(s2, s1);
printf("%s\n", s2);
free(s2);
return 0;
}
42.10 심사문제: 문자 배열 복사하기
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
int main(void)
{
char s1[31];
char s2[31];
printf("문자열을 입력해주세요>> ");
scanf("%s", s1);
printf("\n");
strcpy(s2, s1);
printf("s1: %s\n", s1);
printf("s2: %s\n", s2);
return 0;
}
42.11 심사문제: 두 문자열 붙이기
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
int main(void)
{
char s1[40];
printf("문자열을 입력해주세요>> ");
scanf("%s", s1);
printf("\n");
strcat(s1, "th");
printf("%s\n", s1);
return 0;
}