• 카테고리

    질문 & 답변
  • 세부 분야

    프로그래밍 언어

  • 해결 여부

    미해결

7.8 문제 질문입니다!

23.02.01 15:33 작성 조회수 207

0

#define CRTSECURE_NO_WARNINGS

#include <stdio.h>

#define wjd '.'

int main()

{

char ch;

int count = 0;

int word = 1;

int line = 1;

printf("Enter text : \n");

while ((ch = getchar()) != wjd)

{

if (ch != ' ')

{

++count;

}

else if (ch == ' ') {

++word;

}

else if (ch == '\n') {

++line;

}

}

printf("Chara = %d, Words = %d, Lines = %d", count, word, line);

return 0;

}

 

제 코드는 제대로 실행이 안되던데 코드에 문제가있나요?

답변 1

답변을 작성해보세요.

0

강민철님의 프로필

강민철

2023.02.03

정확히 어떤 문제가 있고 (어떤 오류가 있고)

어떤 시도를 했는지 말씀해주시면 더 명확한 답변이 가능합니다.

ch != ' ', ch == ' ' 대신

아래 코드를 기준(isspace)으로 다시 생각해보세요.

	while ((c = getchar()) != STOP)
	{
		if (!isspace(c))
			n_chars++;	

		if (!isspace(c) && !line_flag)	// !isspace(c) : consider a line which contains a space
		{
			n_lines++;
			line_flag = true;
		}

		if (c == '\n')
			line_flag = false;

		if (!isspace(c) && !word_flag)
		{
			n_words++;
			word_flag = true;
		}

		if (isspace(c))
			word_flag = false;
	}

	printf("Characters = %d, Words = %d, Lines = %d\n", n_chars, n_words, n_lines);

	return 0;