Day1_대소문자 바꿔서 출력

Subin·2024년 6월 26일

Algorithm

목록 보기
1/69

[내 풀이]

#include <iostream>
#include <string>

using namespace std;

int main(void) {
    string str;
    cin >> str;

    for(int i=0; i < str.length(); i++){
        if(str[i] >= 65 && str[i] <= 90)
            str[i] += 32;
        else if(str[i] >= 97 && str[i] <= 122)
            str[i] -= 32;
    }

    cout << str << endl;

    return 0;
}

[다른사람 풀이]


#include <iostream>
#include <string>

using namespace std;

int main(void)
{
    string str;
    cin >> str;
    for (auto c : str)
    {
        if ('a' <= c && c <= 'z')
            c -= 'a' - 'A';
        else
            c += 'a' - 'A';
        cout << c;
    }
    return 0;
}

  1. int i를 이용해서 str 길이를 도는 것보다
    auto c를 만들어서 str을 돌도록 하는 것이 더 깔끔.

  2. 'a'랑 'A'를 10진수로 변경해서 계산하는 것보다 그냥 ' '(작은 따옴표) 사이에 넣어서 계산하면 간단함.

profile
성장하며 꿈꾸는 삶을 살아가고 있는 대학생입니다😊

0개의 댓글