Desc :

char* get(char* buffer);

 

문자열을 읽어서 저장하기 위한 문자열 버퍼를 인수로 받는다

문자열 끝은 null로 종료되므로 배열크기의 -1 자까지만 읽을 수 있다.

 

gets함수의 반환값은 번지값이므로

반환값을 받을 문자형 포인터 변수로 받는다.


Source Code :

#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 != NULL)
	{
		if(*str =='a')
		{
			cnt++;

			str++;

		}
	}
	return cnt;
}

 


Result :

'C' 카테고리의 다른 글

[C] 057 문자열 복사하기 strcpy  (1) 2021.01.25
[C] 056 문자열 출력하기  (1) 2021.01.25
[C] 054 정수값 출력하기 printf()  (1) 2021.01.24
[C] 053 정수값 입력받기 scanf()  (1) 2021.01.24
[C]052 문자 출력하기 putch()  (2) 2021.01.23

+ Recent posts