-2

This may be basic, please be patient with me,

char data[]="(xxx)";

I would like to remove the () and leave data only to be xxx ,

char *p = strtok(data,"(");
p = strtok(NULL,")");
printf("this data: %s \n",p);
printf("this data: %s \n",data);
  1. p should be xxx but its NULL , data stay the same (strtok should affect the source?)
  2. this will not change data, I would like a way to manipulate data to be xxx
machine_1
  • 4,266
  • 2
  • 21
  • 42
Curnelious
  • 1
  • 16
  • 76
  • 150

5 Answers5

3

How about

memmove(data, data + 1, sizeof data - 1);  // Removes the (
data[strlen(data) -1] = '\0';  // Removes the )

Note that the above solution only works on the data as initially presented, with a leading and trailing parentheses. It also only works if data is actually an array and not a pointer. And the array data needs to be in modifiable memory (e.g. RAM).

Also note that is the requirements in the paragraph is met, then the order of the two statements doesn't matter.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
1

It's a char[], not string. You can try something like this :

char new_Data[sizeof(data)/sizeof(char)];
for(i=1 ; i < sizeof(new_Data) ; i++){
        new_Data[i] = data[i];
}

This may work only if "(" and ")" are at the end of your char*

M.Ferru
  • 400
  • 1
  • 6
  • 20
  • "It's a char*, not a string." 1) OP never mentioned either of those, so what are you talking about? 2) It's not a `char*`, it's a `char[]`. Huge difference. –  Jul 31 '16 at 14:08
  • Sorry. I just edit my post – M.Ferru Jul 31 '16 at 14:11
  • thanks, this is not answering my question , its a new var, and also why strtok not affecting data? – Curnelious Jul 31 '16 at 14:14
1
char *in_parens(char* str) {
   char *end, *start;
   if (!(end = strrchr(str, ')'))) return NULL;
   if (!(start = strchr(str, '('))) return NULL;
   *end = '\0';
   return start + 1;
}

int main(void) {
    char data[] = "a(x(b)x)c";
    printf("%s\n", in_parens(data)); // prints x(b)x
}

Returns NULL if string doesn't contain a () pair. Otherwise, strips outermost pair and returns what's inside.

a3f
  • 8,517
  • 1
  • 41
  • 46
0

Your code will not work because strtokdoes not work the way you think it does. When called the first time like this:

char *p = strtok(data,"(");

It will:

  1. skip until the first char that's not a delimiter.
  2. then try to find the next delimiter.
  3. replace the last delimiter with a \0.
  4. return a pointer to the first char of the token.

Since step two fails (there are no more delimiters) strtok will simply return NULL indicating that it didn't find any tokens. Any further calls to strtok for the same input will also return NULL.

You can change this by adding the ending delimiter in the first call like this:

char *p = strtok(data,"()");

Now strtok will find a token, and return a pointer to the first x in your example. Notice though, that since it only replaced the terminating delimiter with a \0, the opening parentesis is still present in data.

To get rid of that one you could do something like this:

char *p = strtok(data,"()");
strcpy(data, p);

However I would rather recommend building a new string for the results if you have any other content in the source data as well.

harald
  • 5,976
  • 1
  • 24
  • 41
-1

The code takes two strings, which first is the complete one, the other is which characters to being deleted. You can specify like ()qwer.

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

char *strip_chars(const char *string, const char *chars)
{
    char * newstr = malloc(strlen(string) + 1);
    int counter = 0;

    for ( ; *string; string++) {
        if (!strchr(chars, *string)) {
            newstr[ counter ] = *string;
            ++counter;
        }
    }

    newstr[counter] = 0;
    return newstr;
}

int main()
{
    char *new = strip_chars("(xxx)", "()");
    printf("%s\n", new);

    free(new);
    return 0;
}