학습 내용 : scanf() 함수를 사용하지 않고, 문자열을 키보드로부터 입력받는 방법을 학습합니다.

 

소스 코드 :

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
#include <stdio.h>
 
int count(char *str);
 
void main()
{
    char string[100];
    char* ret;
 
    ret = gets(string);
 
    if (ret != NULL)
    {
        printf("문자 'a'의 갯수는 %d개입니다.\n", count(string));
    }
}
 
int count(char *str)
{
    int cnt = 0;
 
    while (*str != (int)NULL)
    {
        if (*str++ == 'a')
        {
            cnt++;
        }
    }
 
    return cnt;
}
 

 

 

실행 화면 :