sample.txtを読み込み、デリミタで区切った各単語を配列char **wordsの各要素に格納したいのですが、char.explode関数の中身がおそらく間違えていて実行できません。他の関数内も正直自信がなく手一杯です。どうかご教授お願いいたします。
sample.txtの中身↓
C
1Anyone who has never made a mistake has never tried anything new
実行コード↓
C
1#include <stdio.h> 2#include <string.h> 3#include <stdlib.h> 4//#include <math.h> 5 6 7void read_text(char *fn, char *text){ 8 FILE *fp = fopen(fn, "r"); 9 fgets(text, 9999, fp); 10 fclose(fp); 11} 12 13int count_words(char *text, const char *delim){ 14char *p = NULL; 15for(int count = 0; ; count++){ 16p = strstr(text, delim); 17if(p == NULL) return count+1; 18text = p+1; 19 '\0' 20 } 21} 22 23char **explode(char *text, const char *delim){ 24 char *p = NULL; 25for(char explode = 0;explode++){ 26p = strstr(text, delim); 27if(p == NULL) return explode; 28text = p+1; 29 } 30} 31int main(int argc, char *argv[]){ 32 char *txt = (char *) calloc(10000, sizeof(char)); 33 read_text(argv[1], txt); 34 printf("入力文字列:%s", txt); 35 char *delim = argv[2]; 36 int num_of_words = count_words(txt, delim); 37 printf("単語数:%d\n", num_of_words); 38}
エラーメッセージ↓
cpgm/explode.c: In function ‘count_words’:
cpgm/explode.c:20:4: error: expected ‘;’ before ‘}’ token
}
^
cpgm/explode.c: In function ‘explode’:
cpgm/explode.c:25:32: error: expected ‘;’ before ‘)’ token
for(char explode = 0;explode++){
^
cpgm/explode.c:27:22: warning: return makes pointer from integer without a cast [-Wint-conversion]
if(p == NULL) return explode;
このような実行結果にしたい
./cpgm/explode data/sample.txt " "
入力文字列:Anyone who has never made a mistake has never tried anything new
単語数:12
1 Anyone
2 who
3 has
4 never
5 made
6 a
7 mistake
8 has
9 never
10 tried
11 anything
12 new
回答4件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。