私は割り当てを手掛けようとしています。アイデアは、文字列の配列とファイルストリームを取得することです。私はファイル内のそれらの文字列を探し、これらの文字列の出現を数える必要があります。fgetsとstrstrを使ってCの文字列を見つけて数えよう
私は基本的なループを持っていると思う。唯一の問題は、行中に文字列が見つかったときに、見つかった文字列が始まる位置から1から始まって、その文字列を再度検索したいということです(2回以上出現した場合)。
#define LINE_MAX_CHARS 1000
// n = number of strings to be found
// **strings = array of strings to look for in file
void count_occurrences (int n, FILE *file, char **strings) {
char str[LINE_MAX_CHARS]; // buffer
int count = 0;
while (fgets(str, LINE_MAX_CHARS, file) != NULL){ // for each line
for (int i = 0; i < n; i++){ // for each word in line
char *found;
found = (strstr(str, (*(strings + i)))); // search line
if (found != NULL){ // if word found in line
// here, I want str (the buffer) to have its 0-th element to be the element at (found + 1),
// and the 1-st element to be (found + 2) and so on...
i--; // to look for same word in the rest of the line
count = count + 1;
}
}
}
}
もう1つの問題は、自分のコードをテストする方法がないことです。私はちょうど実行され、私のコードが正しい出力を生成しているかどうか私に教えるテストプログラムを与えられています。
私はfgetsとstrstrを使用する必要があります。
提案?
'(*(strings + i))'してください。インデックスが作成されています。 – wildplasser
@wildplasser私は、あなたが私を殴ってしまったと気づく前に、文字通りほぼ何かを書こうとしていました:P –
私はCを初めて熟知しています。それは同じ出力、右を与える必要がありますか? –