-1

I am trying to remove a certain part of my string using strncpy but I am facing some issues here.

This is what my 2 char* has.

trimmed has for example "127.0.0.1/8|rubbish|rubbish2|" which is a prefix of a address.

backportion contains "|rubbish|rubbish2|"

What I wanna do is to remove the backportion of the code from trimmed. So far I got this:

char* extractPrefix(char buf[1024]){
int count = 0;
const char *divider = "|";
char *c = buf;
char *trimmed;
char *backportionl;
while(*c){
    if(strchr(divider,*c)){
        count++;

        if(count == 5){
            ++c;
            trimmed = c;

            //printf("Statement: %s\n",trimmed);
        }
        if(count == 6){
            backportionl = c;
        }
    }
    c++;
}
strncpy(trimmed,backportionl,sizeof(backportionl));
printf("Statement 2: %s\n", trimmed);

Which nets me an error of backportionl being a char* instead of a char.

Is there anyway I can fix this issue or find a better way to trim this char* to get my aim?

Bocky
  • 483
  • 1
  • 7
  • 27

3 Answers3

2

Here's one way that works for a list of dividers, similar to how strtok works the first time it's called:

char *extractPrefix(char *buf, const char *dividers)
{
    size_t div_idx = strcspn(buf, dividers);
    if (buf[div_idx] != 0)
        buf[div_idx] = 0;
    return buf;
}

If you don't want the original buffer modified, you can use strndup, assuming your platform supports the function (Windows doesn't; you'd need to code it yourself). Don't forget to free the pointer that is returned when you're done with it:

char *extractPrefix(const char *buf, const char *dividers)
{
    size_t div_idx = strcspn(buf, dividers);
    return strndup(buf, div_idx);
}

Alternatively, you could just return the number of characters (or some value less than 0 if the number of characters in the prefix won't fit in an int):

int pfxlen(const char *buf, const char *dividers)
{
    size_t div_idx = strcspn(buf, dividers);
    if (div_idx > (size_t)INT_MAX)
        return -1;
    return (int)div_idx;
}

and use it like this:

int n;
const char *example = "127.0.0.1/8|rubbish|rubbish2|";

n = pfxlen(example, "|");
if (n >= 0)
    printf("Prefix: %.*s\n", n, example);
else
    fprintf(stderr, "prefix too long\n");

Obviously you have a number of options. It's really up to you which one you want to use.

0

Disclaimer: I'm not sure whether I correctly understood what you actually want to achieve. Some examples would probably be helpful.


I am trying to remove a certain part of my string [..]

I have no idea what you're trying in your code, but this is pretty easy to achieve with strstr, strlen and memmove:

First, find the position of the string you want to remove using strstr. Then copy what's behind that found string to the position where the found string starts.

char cut_out_first(char * input, char const * unwanted) {
  assert(input); assert(unwanted);
  char * start = strstr(input, unwanted);
  if (start == NULL) {
    return 0;
  }
  char * rest = start + strlen(unwanted);
  memmove(start, rest, strlen(rest) + 1);
  return 1;
}
Daniel Jour
  • 15,896
  • 2
  • 36
  • 63
0

Welp, this is stupid but i fixed my issue in basically one line. so here goes,

trimmed[strchr(trimmed,'|')-trimmed] = '\0';
printf("Statement 2: %s\n", trimmed);

So by getting the index of 'backportion' from the trimmed char* using strchr, i was effectively able to fix the issue.

Thanks internet, for not much.

Bocky
  • 483
  • 1
  • 7
  • 27
  • 2
    You might want to check that strchr( ) didn't return NULL. If it does, you're going to pass a negative index for the array index. Daniel Jour's answer below illustrates this. – Dave Newman Mar 06 '16 at 19:31
  • 2
    `trimmed[strchr(trimmed,'|')-trimmed]` means the same as `*strchr(trimmed,'|')` – M.M Mar 06 '16 at 19:41