Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define MAX_LINE_LEN 100
- #define MAX_WORDS 100
- #define DELIMITERS "- ,.!?:;*()\n\t"
- char *mystrdup(const char *src);
- int exist(char *list[], int list_num, const char *word);
- int main(int argc, char *argv[]){
- FILE *fp;
- char buf[MAX_LINE_LEN];
- char *found_words[MAX_WORDS];
- int found_word_cnt = 0;
- char *token;
- int token_len;
- /* 入力エラー */
- if(argc != 2){
- fprintf(stderr, "usage: %s input_file_name\n", argv[0]);
- exit(1);
- }
- if( (fp=fopen(argv[1], "r")) == NULL ){
- fprintf(stderr, "error: invalid filename input\n");
- exit(1);
- }
- while(fgets(buf,MAX_LINE_LEN,fp) != NULL){
- token = strtok(buf,DELIMITERS);
- while(token != NULL){
- token_len = strlen(token);
- /* 前後の"'"を削除 */
- if(token[0] == '\''){
- token++;
- token_len--;
- }
- if(token_len > 0 && token[strlen(token)-1] == '\''){
- token[strlen(token)-1] = '\0';
- token_len--;
- }
- /* 既知リストへの登録判断&登録 */
- if( (token_len != 0) &&
- (exist(found_words, found_word_cnt, token) == 0 ) ){
- found_words[found_word_cnt++] = mystrdup(token);
- printf("%3d:%s\n", found_word_cnt, token);
- }
- token = strtok(NULL,DELIMITERS);
- }
- }
- printf("word count: %d\n", found_word_cnt);
- return 0;
- }
- /* srcと同じ文字列をmallocで用意してそのポインタを返す */
- char *mystrdup(const char *src)
- {
- char *p;
- if(src == NULL){
- return NULL;
- }
- p = (char *)malloc(strlen(src) + 1);
- if(p != NULL){
- strcpy(p, src);
- }
- return p;
- }
- /* list内にwordがあれば1、なければ0を返す */
- int exist(char *list[], int list_num, const char *word){
- int i;
- for(i=0;i<list_num;i++){
- if(strcmp(list[i],word)==0) return 1;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement