0

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".

Zippy
  • 3
  • 3
  • 1
    Post a [mcve]. What is `d`? Variables `n`, `j`, and `k` are local variables inside `task`, and `maus` doesn't seem to be used. – vgru Apr 17 '19 at 21:14
  • That's not your only problem [`strcpy`](https://en.cppreference.com/w/c/string/byte/strcpy) has undefined behavior if source and target string buffers overlap (and yours clearly do). I also find the calculations to eventually set `n` (and for that matter `i,j,k`) pointless in `task`, since those variables are all local to the function and have absolutely no bearing on any global variables of the same name. – WhozCraig Apr 17 '19 at 21:15
  • 2
    Never ***ever*** use `gets`! It's a [dangerous](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used) function which have even been removed from the C specification. Use e.g. [`fgets`](https://en.cppreference.com/w/c/io/fgets) instead (but be aware of its semantic differences from `gets`). – Some programmer dude Apr 17 '19 at 21:17
  • As for your problem, this seems like a very good time to [learn how to debug your programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Some programmer dude Apr 17 '19 at 21:18
  • Oh and you seem to be using global variables. Don't do that. Not even for small simple programs like this. – Some programmer dude Apr 17 '19 at 21:19

1 Answers1

1

Your program does not compile, you suppose you can access in main of local variable j of task, d is unknown etc, and you use strcpy while source and destination may overlap and deprecated gets

A proposal using strchr, strrchr and memmove :

#include <stdio.h>
#include <string.h>

int main()
{
  puts("Text : ");

  char s[256];

  if (fgets(s, sizeof(s), stdin) == NULL)
    return -1;

  /* may be remove \n from s */

  char * p1 = strchr(s, '(');

  if (p1 == NULL)
    fprintf(stderr, "'(' is missing\n");
  else {
    char * p2 = strrchr(p1+1, ')');

    if (p2 == NULL)
      fprintf(stderr, "')' is missing\n");
    else {
      memmove(p1, p2 + 1, strlen(p2 + 1) + 1);
      puts(s);
    }
  }

  return 0;
}

Compilation and execution :

pi@raspberrypi:/tmp $ gcc -pedantic -Wall -Wextra p.c
pi@raspberrypi:/tmp $ ./a.out
Text : 
aze(qsd)wxc
azewxc

Note all between the first '(' and last ')' is removed even there are more than one '(' or ')' :

pi@raspberrypi:/tmp $ ./a.out
Text : 
aze((qsd)wxc
azewxc

pi@raspberrypi:/tmp $ ./a.out
Text : 
aze(qsd)iop)wxc
azewxc
bruno
  • 32,421
  • 7
  • 25
  • 37