[내 풀이]
#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;
}
int i를 이용해서 str 길이를 도는 것보다
auto c를 만들어서 str을 돌도록 하는 것이 더 깔끔.
'a'랑 'A'를 10진수로 변경해서 계산하는 것보다 그냥 ' '(작은 따옴표) 사이에 넣어서 계산하면 간단함.