Mentoring
[멘토링][c언어]백준 1152번 : 단어의 개수
seomj
2020. 5. 28. 18:35
#include <stdio.h>
#include <string.h>
void main() {
char str[1000001];
int num=0;
int len;
gets_s(str, 1000001);
len = strlen(str);
for (int i = 0; i < len; i++) {
if (str[i] == ' ') {
num++;
}
}
num += 1;
if (str[0] == ' ') {
num--;
}
printf("%d\n", num);
return 0;
}
str에 문자열을 입력받고 for문을 사용하여 공백을 세주고 공백에 +1을 해주면 단어의 개수를 알 수 있다. 맨 앞에 공백을 넣어줄 수도 있으므로 if 조건문을 사용하여 조건을 따져준다.