<알고리즘 문제풀이&연습>/[C++] 백준, 프로그래머스 등등

[백준 10820] 문자열 분석

BlockDMask 2017. 11. 21. 15:22
반응형
  • 안녕하세요. BlockDMask 입니다.

  • 오늘의 문제 풀어보겠습니다.

0. 제목

  • 백준 10820 문자열 분석

  • BOJ 10820 문자열 분석

  • C++ 문자열

  • C 문자열

1. 문제 설명

  • 문자열 N개가 주어진다.

  • 이 때, 문자열에 포함되어 있는 소문자, 대문자, 숫자, 공백의 개수를 구하는 프로그램을 작성하시오.

  • 각 문자열은 알파벳 소문자, 대문자, 숫자, 공백으로만 이루어져 있다.

  • 입력
    첫째 줄부터 N번째 줄 까지 문자열이 주어진다. (1<=N<=100)
    각 문자열의 길이는 100을 넘지 않는다.

  • 출력
    첫째 줄부터 N번째 줄까지 각각의 문자열에 대해서
    소문자, 대문자, 숫자, 공백의 개수를 공백으로 구분해 출력한다.

2. 풀이 과정

  • 문자열 한 줄을 받아 내기 위해서 getline 을 이용하였습니다.


3. 소스 코드


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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//https://www.acmicpc.net/problem/10820
//BOJ_10820_stringAnalysis
 
#include<iostream>
#include<cstdio>
#include<string>
 
using namespace std;
 
class myString{
private:
    string str; //문자열
    int lower;  //소문자
    int upper;  //대문자
    int digit;  //숫자
    int space;  //띄어쓰기
public:
    myString(string str):str(str){
        lower=upper=digit=space=0;
    }
    void strAnalysis(){
        for(int i=0; i<str.size(); i++){
            if(str[i] == ' '){
                space++;
            }else if(isdigit(str[i])){
                digit++;
            }else if(str[i]>='A' && str[i] <='Z'){
                upper++;
            }else{
                lower++;
            }
        }
    }
    void printAll() const{
        printf("%d %d %d %d\n", lower, upper, digit, space);
    }
};
 
int main(void){
    while(1){
        string str;
        getline(cin, str);
        if(str==""break;
        myString *= new myString(str);
        s->strAnalysis();
        s->printAll();
        delete s;
    }
    return 0;
}
cs


4. 인증


문제출처 - https://www.acmicpc.net/problem/10820

감사합니다.

반응형