https://school.programmers.co.kr/learn/courses/30/lessons/12951
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
알게된 것
- toupper, tolower, isupper, islower <cctype>
- 위의 함수 사용 시 alpha, 공백은 체크하지 않아도 된다. 알아서 해준다.
원래 2번 시도했는데 실패하여 다른 풀이를 참고했다.
내 코드
#include <string>
#include <vector>
#include <sstream>
#include <iostream>
#include <cctype>
#include <algorithm>
using namespace std;
string solution(string s) {
string answer = "";
s[0] = toupper(s[0]);
for(int i = 1; i < s.length(); i++) {
if(s[i-1] == ' ') {
s[i] = toupper(s[i]);
} else {
s[i] = tolower(s[i]);
}
}
answer = s;
return answer;
}
친구 리뷰
- 리턴을 바로하자
시도
// // 44
// string solution(string s) {
// string answer = "";
// // 입력받기
// string str;
// stringstream ss(s);
// while(ss >> str) {
// if(isdigit(str[0]) == 0) {
// if (islower(str[0]))
// str[0] = toupper(str[0]);
// }
// for(int i = 1; i < str.size(); i++) {
// if (isupper(int(str[i])))
// str[i] = tolower(str[i]);
// }
// // str.erase(remove(str.begin(), str.end(), ' '), str.end());
// answer += str + " ";
// }
// if (s[s.length() - 1] != ' ' && s.length() > 1)
// answer = answer.substr(0, answer.length() - 1);
// return answer;
// }
// 3개만 맞는다../;;;
// string solution(string s) {
// string answer = "";
// bool isPreviousSpace = true;
// int i = 0;
// while(i < s.size()) {
// if(s[i] == ' ') {
// isPreviousSpace = true;
// }
// else if (!isdigit(s[i])) {
// if(isPreviousSpace && islower(s[i])) {
// s[i] = toupper(s[i]);
// isPreviousSpace = false;
// }
// else if (!isPreviousSpace && isupper(s[i])) {
// s[i] = tolower(s[i]);
// isPreviousSpace = false;
// }
// } else {
// isPreviousSpace = false;
// }
// answer += s[i];
// i++;
// }
// return s;
// }
728x90
반응형
'자료구조 알고리즘 > 코딩테스트' 카테고리의 다른 글
최솟값 만들기 (0) | 2022.09.16 |
---|---|
이진 변환 반복하기 (0) | 2022.09.16 |
최댓값과 최솟값 (0) | 2022.09.16 |
1251. Average Selling Price (0) | 2022.06.24 |
1565. Unique Orders and Customers Per Month (0) | 2022.06.24 |