My task is to find and delete text in parentheses from string. My idea is to count the position of first '(' and last ')' and afterwards delete d characters from '(' position, problem is, position of '(' and ')' is replaced by 0 if there is something actually in the parentheses.
void task(char *s)
{
int i,d;
int j=0; //position of first '('
int k=0; //add 1 for every character in parentheses until reach ')'
for(i=0; i<strlen(s); i++)
{
if(s[i]=='(')
{
j=i;
}
else{
if(s[i]==')')
k=i;
printf("k=%d \n",k);
}
}
d=(k-j-1);
}
void deleteptext(char *x,int a, int b)
{
if((a+b-1)<=strlen(x))
{
strcpy(&x[b-1],&x[a+b-1]);
puts(x);
}
}
int main()
{
puts("Text: ");
gets(s);
task(s);
deleteptext(s,j,d);
}
For example, if my input is abc (def)
, output is the same(need abc
), 'j' value at one point is 4, but it is returned to 0 as it comes across "d".