학습 내용 : 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;
}
|
실행 화면 :

'프로그래밍 > C언어 300제' 카테고리의 다른 글
중급 57. 문자열 복사하기(strcpy) (0) | 2020.02.17 |
---|---|
중급 56. 문자열 출력하기 (puts) (0) | 2020.02.17 |
중급 54. 정수값 출력하기 (printf) (0) | 2020.02.17 |
중급 53. 정수값 입력받기 (scanf) (0) | 2020.02.17 |
중급 52. 문자 출력하기 (putch) (0) | 2020.02.17 |
트랙백 , 댓글 가 달렸습니다.