
한 개의 문자열을 입력받아 해당 문자열에 알파벳 대문자가 몇 개 있는지 알아내는 프로그램을 작성하세요.
- 첫 줄에 문자열이 입력된다. 문자열의 길이는 100을 넘지 않습니다.
- 첫 줄에 대문자의 개수를 출력한다.
- 입력 예) KoreaTimeGood
- 출력) 3
풀이
const solution = (str) => {
let count = 0
const test = str.toUpperCase()
for(let i = 0; i < str.length; i++){
if(test[i]===str[i]){count++}
}
return count
}
solution("KoreaTimeGood")
동료의 풀이
function solution2(s) {
const reg = /[A-Z]/g
return s.match(reg).length
}
동료의 풀이2
function solution(input) {
const start = "A".charCodeAt();
const end = "Z".charCodeAt();
let result = 0;
for (let i = 0; i < input.length; i++){
if(input.charCodeAt(i) >= start && input.charCodeAt(i) <= end) result++;
}
return result;
}'Algorithm' 카테고리의 다른 글
| 대소문자 변환 (0) | 2022.11.20 |
|---|---|
| [algorithm] 대문자로 통일 (0) | 2022.11.20 |
| [algorithm] 문자 찾기 (0) | 2022.11.06 |
| [algorithm] A를 #으로 (0) | 2022.11.06 |
| [algorithm] 일곱 난쟁이 (0) | 2022.10.29 |