단어 개수세기 프로그램
아롱별
#include stdio.h
#include string.h
#include ctype.h
#include stdlib.h
#define MAXWORD 100
void convert_lower(char *str);
void insert_data(int index, char *str);
void intialize();
struct WORDCOUNT
{
char *str;
int count;
};
struct WORDCOUNT *words;
int nwords;
void main()
{
char buffer[256];
char *token;
int found, index;
char seps[]= ,\t\n;
int words=0;
FILE *fp = fopen(test.txt,r);
while(fgets(buffer,255,fp) !=NULL)
{
printf(%s,buffer);
token=strtok(buffer,seps);
while(token!=NULL)
{
printf(%s,token);
convert_lower(buffer);
insert_data(index,token);
index++;
intialize();
token=strtok(NULL,seps);
}
}
fclose(fp);
}
void convert_lower(char *str)
{
while(*str !=NULL)
{
*str=tolower(*str);
str++;
}
}void intialize()
{
int n;
words= (struct WORDCOUNT*)malloc(sizeof(struct WORDCOUNT)*MAXWORD);
nwords=0;
memset(words,0,sizeof(struct WORDCOUNT)*MAXWORD);
}
void insert_data(int index, char *str)
{
int size;
if(nwordsMAXWORD-1)
{
size=strlen(str)+1;
words[index].str=(char*)malloc(size);
strcpy(words[index].str,str);
words[index].count=1;
nwords+=1;
}
}
void print_words()
{
int n;
for(n=0;nnwords;n++)
printf(%3d, %-18s: %d\n,n+1,words[n].str,words[n].count);
}
파일을 읽어서 단어개수를 세는 프로그램인데요. 자꾸 에러가 나느데 어디가 잘못된건지 ㅠㅠ 알수가 없네요.