[백준] 10820 - 문자열 분석 (Python)

민영·2021년 9월 1일
0

[Algorithm] 백준

목록 보기
13/31
post-thumbnail

문제

https://www.acmicpc.net/problem/10820

제출 코드

import sys

while True:
    line = sys.stdin.readline().rstrip("\n")
    low, up, num, space = 0, 0, 0, 0

    if not line:
        break

    for s in line:
        if s.islower():
            low += 1
        elif s.isupper():
            up += 1
        elif s.isdigit():
            num += 1
        elif s.isspace():
            space += 1

    print(low, up, num, space)
    

결과


정리

KEY POINT

이 문제에서는 한줄씩 입력받을 때 rstrip("\n")로 해야 된다. rstrip("")로 하게 되면 space도 제거되기 때문이다. 공백의 개수를 구해야 되기 때문에 공백은 남겨야 된다!

주석있는 코드

import sys

while True:
    line = sys.stdin.readline().rstrip("\n")
    low, up, num, space = 0, 0, 0, 0

    # 다음 줄이 없으면 끝내기
    if not line:
        break

    # 소문자, 대문자, 숫자, 공백 구하기
    for s in line:
        if s.islower():
            low += 1
        elif s.isupper():
            up += 1
        elif s.isdigit():
            num += 1
        elif s.isspace():
            space += 1

    print(low, up, num, space)
profile
그날의 기록

0개의 댓글