私は、C言語でプログラムを書いた期待される結果がどうあるべき文字列をリセットしません:私をtempWord [0] = ' 0' 何とか
$ ./censor Ophelia < poem.txt
Said Hamlet tomlet CENSORED,
I'lllia drawlia arawlia sketcha ofetcha theecha,
Whatcha kindcha ofndcha pencila shallla Ihallla usellla?
2Bsellla orellla notllla 2Botllla?
:
$ cat poem.txt
Said Hamlet to Ophelia,
I'll draw a sketch of thee,
What kind of pencil shall I use?
2B or not 2B?
$ ./censor Ophelia < poem.txt
Said Hamlet to CENSORED,
I'll draw a sketch of thee,
What kind of pencil shall I use?
2B or not 2B?
しかし、私はこれを得ましたtempWordを使用してすべての単語を保存し、それを打ち切る必要がある単語と比較します。次に、別の比較を行うために、tempWord[0]='\0'
を使用して一時文字列をリセットします。しかし、それは動作していないようです。誰も助けることができますか?
# include <stdio.h>
# include <string.h>
int compareWord(char *list1, char *list2);
int printWord(char *list);
int main(int argc, char *argv[]) {
int character = 0;
char tempWord[128];
int count = 0;
while (character != EOF) {
character = getchar();
if ((character <= 'z' && character >= 'a') ||
(character <= 'Z' && character >= 'A') ||
character == 39) {
tempWord[count] = character;
count++;
} else {
if (count != 0 && compareWord(tempWord, argv[1])) {
printf("CENSORED");
count = 0;
tempWord[0] = '\0';
}
if (count != 0 && !compareWord(tempWord, argv[1])) {
printWord(tempWord);
count = 0;
tempWord[0] = '\0';
}
if (count == 0) {
printf("%c", character);
}
}
}
return 0;
}
int printWord(char *list) {
// print function
}
int compareWord(char *list1, char *list2) {
// compareWord function
}
あなたはラインによってあなたのプログラム行の実行をトレースするために、デバッガを使用して提案します。 – kaylum
'strlen()'はC- "文字列"とみなされます。つまり、*任意の*場合は、 'char'配列が' 0'に渡されたことを意味します。 – alk
@alk:正しいです。 memsetを使用してすべての位置をnullに設定した場合。その後、この問題も解決されます。 – sureshkumar