출처 :44.7 심사문제: 공백 개수 세기 (dojang.io)
표준 입력으로 길이 1,000 이하의 문자열이 입력됩니다. 입력된 문자열에서 공백의 개수를 출력하는 프로그램을 만드세요(scanf 함수 호출 전에 문자열을 출력하면 안 됩니다).
scanf에서 공백을 포함한 문자열을 입력받으려면 서식 지정자로 "%[^\n]s"를 사용하면 됩니다.
정답에는 C 언어 컴파일러에서 정상적으로 컴파일되는 전체 코드를 입력해야 합니다.
예입력
I raised the bucket to his lips. He drank, his eyes closed. It was as sweet as some special festival treat. This water was indeed a different thing from ordinary nourishment. Its sweetness was born of the walk under the stars, the song of the pulley, the effort of my arms. It was good for the heart, like a present. When I was a little boy, the lights of the Christmas tree, the music of the Midnight Mass, the tenderness of smiling faces, used to make up, so, the radiance of the gifts I received.
결과
94
CODE
RESULT
1. 공백의 개수를 출력하는 문제 = 문장에서 " "을 검색해서 갯수 출력하는 변수에 넣어준다.
2. strstr(대상 문자열, "검색할 문자열");
3. while(ptr!=NULL) // ptr 포인터가 NULL포인터가 아닐때까지
4. count++ // count 변수 1씩 더해줌
5. ptr = strstr(ptr+1," "); // " "(공백) 기준 다음 문자부터 다시 검색
'프로그래밍 > C & C++ & C#' 카테고리의 다른 글
[C] 문자열 자르기 (0) | 2021.07.10 |
---|---|
[C]서수 줄임말 문자열 만들기 (0) | 2021.07.07 |
[C] 단위행렬 만들어보기 (1) | 2021.07.04 |