알고리즘/백준

단어의 개수

슈도코드 2019. 10. 14. 22:55

#include <stdio.h>

int main(void) 
{
	char str[1000000] = { 0 };
	int cnt = 1;

	gets(str); //공백 처리를 위함

	if (str[0] == ' ') //처음
		cnt = cnt - 1;

	for (int i = 0; str[i] != 0; i++) 
	{
		if (str[i] == ' ')
		{
			cnt++;
			if (str[i + 1] == 0)  //마지막
			{ 
				if (str[i] == ' ')
					cnt = cnt - 1;
			}
		}
	}
	printf("%d \n", cnt);
	return 0;
}