カッコ内の文字を削除するプログラムを作成しました。入力されたテキストには、開き、閉じ括弧が一致する必要があります。Cのカッコ内の文字を削除する
ケース1:
入力: (Hello) World
出力:World
ケース2:
入力: (Hello World
出力:(Hello World
ケース3:
入力: Hello)(World
出力:Hello)(World
ケース4:
入力: Hello((hi) World)
出力:Hello
ケース5:
入力: (Hello) hi (World)
出力:hi
は、ここに私のコードです:
#include <stdio.h>
int main(){
char string[100] = {0};
char removedletters[100] = {0};
fgets(string, 100, stdin);
char *p;
int x = 0;
int b = 0;
for (p=string; *p!=0; p++) {
if (*(p-1) == '(' && x) {
x = 0;
}
if (*p == ')') {
x = 1;
}
if (!x){
removedletters[b] = *p;
b++;
}
}
puts(removedletters);
}
ケース1、3、5は何ケース2及び4 に正しいですが、ではありません私のコードに間違っていますか?
使用strchrの代わりに、(*(P-1)は '最初の反復のために未定義の動作であれば、文字列 – thumbmunkeys
'を使用して手動で検索する。 – Mat
1はStackOverflowのへようこそ!グレート最初の質問を。 – phihag