본문 바로가기
Code/C

[c] 문자열 단어 개수 구하기

by Geffi 2022. 11. 6.
더보기

문자열 단어 개수 구하기

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

int main(void)
{
    char p[100] = "a book and pencil";
    int cnt = 0;

    for (int i = 0; i < strlen(p); i++)
    {
        if (p[i] != ' ' && p[i] != '\n')
            cnt++;
        else{
            continue;
        }
    }
    printf("word count: %d\n", cnt);
    return 0;
}
// ctype.h 이용

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

int main(void){
    char s[10000] = "a book is not good for my health";
    int count=0;


    for(int i = 0; i< strlen(s); i++){
        if(isspace(s[i])==0){
            count++;
        }
        else{
            continue;
        }
    }
    printf("word count: %d", count);

    return 0;
    
}

'Code > C' 카테고리의 다른 글

[C] 배열 순서 거꾸로 하기  (0) 2022.11.06
[c] 화씨/섭씨 변환  (0) 2022.11.06