1152번: 단어의 개수
첫 줄에 영어 대소문자와 띄어쓰기로 이루어진 문자열이 주어진다. 이 문자열의 길이는 1,000,000을 넘지 않는다. 단어는 띄어쓰기 한 개로 구분되며, 공백이 연속해서 나오는 경우는 없다. 또한
www.acmicpc.net
문제
코드
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main() {
char str[1000001];
int space = 0;
int word = 0;
int len;
gets(str);
len = strlen(str);
for (int i = 0; i < len; i++) {
if (str[i] == ' ')
space++;
}
word = space +1;
if (len == space) {
word = 0;
printf("%d\n", word);
}
else {
if (isspace(str[0]))
word--;
if (isspace(str[len-1]))
word--;
printf("%d\n", word);
}
}
'Software > C' 카테고리의 다른 글
[Baekjoon C] 10828 스택 (0) | 2021.02.19 |
---|---|
[Baekjoon C] 1259 팰린드롬수 (0) | 2021.02.18 |
[Baekjoon C] 10818 최소, 최대 (0) | 2021.02.15 |
[Baekjoon C] 2753 윤년 (0) | 2021.01.31 |
[Baekjoon C] 2884 알람시계 (0) | 2021.01.31 |