Layer7

-Layer7 코드업 1412 알파벳 개수 출력하기

KSJ._.seven11 2023. 4. 16. 16:17

 fgets를 통해서 풀어봤다 !

 공백 부분까지 해서 계산을 하면 된다 !
 다른 간편한 함수가 있지 않을까 생각이 든다.. 내가 아는 함수들로는 이렇게 짜는게 최대다..

#include <stdio.h>

int main() {
    char str[100];
    int count[26] = {0};
    
    fgets(str, 100, stdin);
    
    for (int i = 0; str[i] != '\0'; i++) {
        if (str[i] >= 'a' && str[i] <= 'z') {
            count[str[i] - 'a']++;
        }
        else if (str[i] >= 'A' && str[i] <= 'Z') {
            count[str[i] - 'A']++;
        }
    }
    
    for (int i = 0; i < 26; i++) {
        printf("%c:%d\n", 'a' + i, count[i]);
    }
    
    return 0;
}