본문 바로가기

ETC Programmings

단어의 갯수를 세는 소스

336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define SIZE 1024

int word_cnt();

int word_cnt(char *string){
 int count = 0;
 
 while(*string != '\0'){ // 문장이 끝날대
  while (isspace(*string)) // 포인터가 가르키는 인덱스가 공백인 경우
   string++; //다음 인덱스로 포인터를 변경
  if(*string != '\0'){
   count++;
  while (!isspace(*string) && *string != '\0')
   string++;
  }
 }
 return count;
}
int main(){
 char string[SIZE];
 printf("입력하신 단어의 수를 세는 프로그램이다?");
 gets(string);
 printf("입력하신 단어의 수는 : %d개 입니다.\n", word_cnt(string));
}


공백을 조건으로 해서 단어의 갯수를 골라준다. 하지만 공백 이외의 알파벳이 아닌것이 나올때는 어떻게 해야 할까..? isalpha() 함수를 사용한다. 집에서 한번 해볼것..