I created a program that will remove the characters inside the brackets. The text entered must have the corresponding open and closing parentheses.
Case 1:
Input: (Hello) World
Output:World
Case 2:
Input: (Hello World
Output:(Hello World
Case 3:
Input: Hello)(World
Output:Hello)(World
Case 4:
Input: Hello((hi) World)
Output:Hello
Case 5:
Input: (Hello) hi (World)
Output:hi
Here is my code:
#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);
}
Case 1, 3 and 5 are true, but not in cases 2 and 4. What is wrong with my code?
user930305
source
share