문제 풀이
[프로그래머스] JadenCase 문자열 만들기
컴영
2020. 10. 1. 16:47
JadenCase 문자열 만들기
문제) https://programmers.co.kr/learn/courses/30/lessons/12951
풀이)
A의 아스키 코드가 65, a의 아스키 코드가 97 임을 이용해서 소문자↔대문자를 바꿔줬는데
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 | #include <string> #include <vector> using namespace std; string solution(string s) { string answer = ""; //A 65 a 97 if(s[0] >= 'a' && s[0] <= 'z'){ answer += s[0] - 32; } else answer += s[0]; for(int i=1;i<s.length();i++){ if(s[i-1] == ' '){//공백다음 문자 -> 소문자일 경우 대문자로 if(s[i] >= 'a' && s[i] <= 'z'){ answer += s[i] - 32; } else answer += s[i]; } else{//문자 내의 대문자일 경우 소문자로 if(s[i] >= 'A' && s[i] <= 'Z'){ answer += s[i] + 32; } else answer += s[i]; } } return answer; } | cs |
tolower(), toupper()함수를 이용하면 더 쉽게 코드를 작성할 수 있었다.
#include <ctype.h>
int tolower(int C) // 대문자 C를 소문자로 바꿔준다.
int toupper(int c) //소문자 c를 대문자로 바꿔준다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | #include <string> #include <vector> using namespace std; string solution(string s) { string answer = ""; answer += toupper(s[0]); for(int i=1;i<s.length();i++){ if(s[i-1] == ' '){ answer += toupper(s[i]); } else{ answer += tolower(s[i]); } } return answer; } | cs |