eoxjf   7년 전

#include <stdio.h>
#include <string.h>

int main()
{
    char a[1000000+1]={0};
    int cnt = 0;
    int i = 0;
    int tf = 0;

    scanf("%[^\n]", a);

    for(i=0; i < 1000000; i++)
    {
        if((a[i] >= 'a' && a[i] <= 'z') || (a[i] >='A' && a[i] <= 'Z'))
        {
            tf = 1;
        }
        else if(a[i] == ' ')
        {
            if(tf)
            {
                cnt++;
                tf = 0;
            }
        }
        else if(a[i] == 0x00)
        {
            if(tf)
            {
                cnt++;
                tf = 0;
            }
            break;
        }
        else
        {
            goto out;
        }

    }
    printf("%d\n", cnt);
    return 0;

out:
    printf("error :%d\n", i);
    return -1;
}

sgchoi5   7년 전

while ((c = getchar()) != EOF && c != '\n') { 로 한 글자씩 처리하는 방식으로 하시고, space 개수로 단어 개수를 알 수 있을 것이고, 시작이나 끝이 space 가 나올 수 있는 것을 고려하시면 될 듯 합니다.

eoxjf   7년 전

와... 정말 단순한 문제였는데 몰랐네요

comment 감사합니다~

댓글을 작성하려면 로그인해야 합니다.