Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
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
관리 메뉴

개발 공부

대소문자 바꾸기 본문

카테고리 없음

대소문자 바꾸기

방구석개발입문 2022. 11. 5. 00:33

string으로 받아서 해결!

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string str;
    
    cin>>str;
    
    for(int i=0; i<str.length(); i++)
    {
        if(str[i] < 'a')                      //a의 아스키코드값이 생각이 안나서 이렇게 풀었음!
            str[i] = tolower(str[i]);
        else if(str[i] >= 'a')
            str[i] = toupper(str[i]);
    }
    
    cout << str;
}

아스키코드 

A : 65

a:  97   

대문자와 소문자의 아스키코드 값 차이는 32!!

>> str[i] += 32를 해줘도 되지만

 

소문자 변환  : tolower()함수

대문자 변환 : toupper()함수  를 사용하면 된다!

 

 

 

 

 

 

x = input()

for i in range(len(x)):
    if(x[i] < 'a'):
        print(x[i].lower(),end='')
    elif (x[i] >= 'a'):
        print(x[i].upper(),end='')
x = input()

for i in x:
    if i.isupper():
        print(i.lower(), end="")
    else:
        print(i.upper(), end="")
print(input().swapcase())

1.

string.lower() : 소문자변환

string.upper() : 대문자 변환

 

2.

.isupper() : 대문자이면 true

.islower():  소문자이면 true

 

3. 가장 간단하게

swapcase(): 대소문자 상호전환!