2023. 9. 29. 23:11, 알고리즘/프로그래머스
https://school.programmers.co.kr/learn/courses/30/lessons/120911
분명 기억속에, 대문자를 소문자로, 소문자를 대문자로 만드는 함수가 있었고, 그게 대략적으로 uppercase, lowercase 등의 키워드로 이루어진 함수였던것 같은데, 막상 해당 함수를 기억해서 사용해보려 하니 생각이 나질 않아서, 아스키코드 상에서 차이를 이용해서 푸는 방법으로 프로그램을 작성하였다.
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
string solution(string str) {
for(auto& c: str){
if(c>=65&&c<=90) c+=32;
}
cout<<str;
sort(str.begin(),str.end());
return str;
}
다른 사람의 풀이를 참고해보자면, 바로 처음에 나오는 풀이가 내가 찾던 lowercase로 만드는 함수를 활용한 풀이였다.
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
string solution(string my_string) {
for(auto& ch : my_string)
{
ch = tolower(ch);
}
sort(my_string.begin(), my_string.end());
return my_string;
}
함수 이름이 기억이 잘 나지 않았는데, tolower() 라는 함수였구나, 대문자로 바꾸려면 toupper() 라는 함수를 활용하면 되겠다.
다음에는 이런 경우에 tolower(), toupper() 함수를 사용해서 문제를 잘 해결해보도록 하자.
'알고리즘 > 프로그래머스' 카테고리의 다른 글
| Lv 0. 잘라서 배열로 저장하기. (0) | 2023.10.01 |
|---|---|
| Lv 0. 7의 개수 (0) | 2023.10.01 |
| Lv 0. 세균 증식 (0) | 2023.09.29 |
| Lv 0. 제곱수 판별하기 (0) | 2023.09.29 |
| Lv 0. 문자열안에 문자열 *다시 풀어보기* **매우중요** (0) | 2023.09.29 |
Comments, Trackbacks
