ElectronAnt
ElectronAnt

Reputation: 2195

Remove a substring in C given positions of substring

Let's say I have a char *example that contains 20 chars. I want to remove every char from example[5] to example[10] and then fix up the array so that example[11] comes right after example[4].

Basically shifting all the characters after the deleted region to when the deleted region started.

Any ideas?

EDIT: I think there might be a way using memcpy? But I'm not sure how to do it.

Upvotes: 0

Views: 3048

Answers (5)

nevsan
nevsan

Reputation: 1445

memmove(&example[5], &example[11], strlen(example) - 11 + 1);

Note that the behavior of memcpy is undefined when the memory blocks overlap, so memmove is more appropriate here.

Upvotes: 0

wildplasser
wildplasser

Reputation: 44220

spoiler:

void cut_the_string(char *str, size_t cutpos, size_t cutlen)
{
  size_t len;

  len = strlen(str);

  if (cutpos >= len) return;
  if (cutpos + cutlen > len) cutlen = len-cutpos;

  memmove( str+cutpos, str+cutpos+cutlen, 1+len - cutpos - cutlen);

  return;
}

Upvotes: 1

Jonathan Leffler
Jonathan Leffler

Reputation: 753475

You can't use memcpy() reliably because there's overlap between the source and target; you can use memmove(). Since you know the lengths, you use:

memmove(&example[5], &example[11], (20 - 11 + 1));

Remember you need to copy the null terminator too.

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

int main(void)
{
    char array[] = "abcdefghijklmnopqrst";
    char *example = array;
    printf("%.2zu: <<%s>>\n", strlen(example), example);
    memmove(&example[5], &example[11], (20 - 11 + 1));
    printf("%.2zu: <<%s>>\n", strlen(example), example);
    return(0);
}

Compiled with a C99 compiler, that yields:

20: <<abcdefghijklmnopqrst>>
14: <<abcdelmnopqrst>>

If you have a C89 compiler (more specifically, C library), you'll have to worry about the z in the format string, which indicates a size_t argument. It's simplest to remove the z and cast the result of strlen() with (unsigned).

Upvotes: 4

Vikdor
Vikdor

Reputation: 24124

Any ideas?

I would prepare a parallel array, copying all the characters from the source array but ignoring those characters in the range specified. Optionally, one can release the memory used by source array if it is not on stack.

Upvotes: 0

emesx
emesx

Reputation: 12755

  1. shift chars in a loop OR
  2. copy chars example+11..example+20, paste at example+4 OR
  3. ....

Upvotes: 0

Related Questions