본문 바로가기

programming

C) 공백 문자 갯수 구하기

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
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
 
int main(void)
{
    char s1[1001= {0};
    char *ptr = NULL;
    int nCnt = 0;
    
    printf("1000자 이하로 입력하세요(알파벳만): ");
    scanf("%[^\n]s", s1);
    
    
    ptr = strchr(s1, ' ');
    while(ptr != NULL)
    {
        nCnt++;
        ptr = strchr(ptr+1' ');
    }
   
    
    printf("공백문자의 갯수: %d\n", nCnt);
    
    return 0;
}
 
cs

scanf("%[^\n]s", s1);


공백 문자를 입력 받으려면 위와 같이 %s 사이에 []브라켓 ^캐럿 \n백슬래쉬엔을 입력해야 한다.