Notice
Recent Posts
Recent Comments
Link
«   2025/07   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

문타쿠, 공부하다.

[C언어 코딩 도장] Unit 42. 연습문제 및 심사문제 본문

C언어/C언어 코딩 도장

[C언어 코딩 도장] Unit 42. 연습문제 및 심사문제

개발새발 문타쿠 2023. 9. 25. 23:04

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;
}