Problem Solving

[백준/C] 1152

Jintiago 2022. 7. 13. 02:13

scanf로 입력받을 시 공백이 포함 안 되는걸 잊어먹고 또 뻘짓했다...

대충 gets는 된다길래 그거 썼다.

 

단어의 수를 세면 된다.

 

split을 구현해봤다면 매우 간단하게 풀 수 있는 문제였다.

 

#include <stdio.h>

int    count_words(char str[])
{
    int    i;
    int    cnt;
    
    i = 0;
    cnt = 0;
    while (str[i] != 0)
    {
        if (str[i] == 32)
            i++;
        if (str[i] != 32 && str[i] != 0)
            cnt++;
        while (str[i] != 32 && str[i] != 0)
            i++;
    }
    return (cnt);
}

int    main(void)
{
    char    str[1000001];

    gets(str);
    printf("%d\n", count_words(str));
    return (0);
}