2022.03.02 문자열_문제

2022. 3. 2. 02:58과제 업로드

문자열 문제 1,2,3번 한번에 작성

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <stdio.h>
#include <string.h>
#define MAX_INPUT 256 //최대 입력 문자 개수 지정
 
int str_chr(char* s, int c) //문자열 내 문자 개수를 알려주는 함수
{
    int i = 0, count =0// 문자열 인덱스 번호 i , 개수 누적을 위한 변수 count
    while (1)
    {
        if (s[i] == 0//문자열 끝을 알리는 \0값을 만나면 무한 루프 종료
        {
            break;
        }
        else if (s[i] == c) //입력한 문자와 문자열 인덱스 위치의 문자가 같으면 개수 누적
        {
            count++;
        }
        else if (s[i] == c + 32)//입력한 대문자와 문자열 인덱스 위치의 소문자가 같으면 개수 누적
        {
            count++;
        }
        else if (s[i] == c - 32)//입력한 소문자와 문자열 인덱스 위치의 대문자가 같으면 개수 누적
        {
            count++;
        }
        i++;
    }
    return count;
}
 
double Calstring(const char* s, double a, double b)
{
    double result= 0;
    if (!strcmp(s,"add")) //두 문자열이 같으면 반환값이 0이므로 not연산자를 이용하여 참값 출력
    {
        result = a + b;
    }
    else if (!strcmp(s, "sub"))
    {
        result = a - b;
    }
    else if (!strcmp(s, "mul"))
    {
        result = a * b;
    }
    else if (!strcmp(s, "div"))
    {
        result = a / b;
    }
    else
    {
        printf("Error\n");
    }
        return result;
}
int main()
{
    //string1
    char input[MAX_INPUT] = { 0 };
    char targetC;
    int result;
    printf("문자열을 입력하시오.\n입력 :");
    gets_s(input,sizeof(input));
    printf("개수를 셀 문자를 입력하시오 :");
    scanf_s("%c"&targetC,1);
    result = str_chr(input, targetC);
    printf("%c의 개수: %d\n", targetC, result);
 
    //string2
    char input2[MAX_INPUT] = { 0 };
    getchar();
    printf("문자열을 입력하시오.\n입력 :");
    gets_s(input, sizeof(input));
    for (int i = 97; i <= 122; i++)
    {
        result = str_chr(input, i);
        printf("%c의 개수: %d\n", i, result);
    }
 
    //string3
    char input3[10];
    double a,b;
    double result2;
    printf("연산을 입력하시오:");
    scanf_s("%s %lf %lf", input3, sizeof(input3), & a, &b);
    result2 = Calstring(input3, a, b);
    printf("연산의 결과: %.2lf", result2);
    return 0;
}
cs