Notice
Recent Posts
Recent Comments
Link
«   2025/06   »
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
Tags
more
Archives
Today
Total
관리 메뉴

문타쿠, 공부하다.

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

C언어/C언어 코딩 도장

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

개발새발 문타쿠 2023. 9. 25. 11:45

41.4 연습문제: 문자열 길이 구하기

#include <stdio.h>
#include <string.h>

int main(void)
{
    char* s1 = "C Language";

    printf("%d\n", strlen(s1));

    return 0;
}

41.5 연습문제: 문자열 비교하기

#include <stdio.h>
#include <string.h>

int main(void)
{
    char* s1 = "Pachelbel Canon";
    char* s2 = "Pachelbel Canon";

    printf("%d\n", strcmp(s1, s2));

    return 0;
}

41.6 심사문제: 문자열 길이 구하기

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>

int main(void)
{
    char s1[30];
    
    printf(">> ");
    scanf("%[^\n]s", s1);
    printf("\n");
    
    printf("%d\n", strlen(s1));

    return 0;
}


41.7 심사문제: 문자열 비교하기

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>

int main(void)
{
    char s1[30];
    char s2[30];

    printf(">> ");
    scanf("%s %s", s1, s2);
    printf("\n");

    int ret = strcmp(s1, s2);

    if (ret == 0)
    {
        printf("두 문자열은 같습니다.(%d)\n", ret);
    }
    else if (ret == 1)
    {
        printf("왼쪽의 문자열(%s)이 더 큽니다.(%d)\n", s1, ret);
    }
    else if (ret == -1)
    {
        printf("오른쪽의 문자열(%s)이 더 큽니다.(%d)\n", s2, ret);
    }

    return 0;
}