대문자와 소문자가 같이 존재하는 문자열을 입력받아 대문자로 모두 통일하여 문자열을 출력하는 프로그램을 작성하세요.
첫 줄에 문자열이 입력된다. 문자열의 길이는 100을 넘지 않습니다.
첫 줄에 대문자로 통일된 문자열이 출력된다.
ItisTimeToStudy
<html>
<head>
<meta charset="UTF-8" />
<title>출력결과</title>
</head>
<body>
<script>
function solution(str) {
return str.toUpperCase()
}
let str = "ItisTimeToStudy"
console.log(solution(str))
</script>
</body>
</html>
<html>
<head>
<meta charset="UTF-8">
<title>출력결과</title>
</head>
<body>
<script>
function solution(s){
let answer="";
for(let x of s){
let num=x.charCodeAt();
if(num>=97 && num<=122) answer+=String.fromCharCode(num-32);
else answer+=x;
//if(x===x.toLowerCase()) answer+=x.toUpperCase();
//else answer+=x;
}
return answer;
}
let str="ItisTimeToStudy";
console.log(solution(str));
</script>
</body>
</html>
아스키코드를 사용해서 변환한다.
x.charCodeAt()
을 사용해서 s의 원소인 x의 아스키코드를 알아내고, String.fromCharCode(num-32)
를 통해 (num-32) 아스키코드를 문자(String)으로 변환한다.
즉, num이 소문자일 때 num-32를 통해 대문자로 변환할 수 있다.