1

I have a string "abcdefg-this-is-a-test" and I want to delete the first 6 characters of the string. This is what I am trying:

char contentSave2[180] = "abcdefg-this-is-a-test";
strncpy(contentSave2, contentSave2+8, 4);

No luck so far, processor gets stuck and resets itself. Any help will be appreaciated.

Question: How can I trim down a string in C?

////EDIT//// I also tried this:

memcpy(contentSave2, &contentSave2[6], 10);

Doesn't work, same problem.

10
  • What is contentSave? It is not defined Commented Sep 23, 2016 at 14:01
  • I'm sorry, my bad. I am just using 1 string array, see edit. Commented Sep 23, 2016 at 14:02
  • Just sum it: ideone.com/5zXUbn Commented Sep 23, 2016 at 14:03
  • 1
    "String array" is very unfortunate terminology. It's a string, and it's an array (of characters, forming the string). It's not a "string array" until it's an array of strings. Commented Sep 23, 2016 at 14:05
  • @Guedes Thank you, this gives the correct output, but how do I store this output into the contentSave2 string? Commented Sep 23, 2016 at 14:07

5 Answers 5

3
int len=strlen(content2save);
for(i=6;i<len;i++)
   content2save[i-6]=content2save[i];

content2save[i-6]='\0'

This will delete first 6 charcters . Based on requirement you may modify your code. If you want to use an inbuilt function try memmove

Sign up to request clarification or add additional context in comments.

Comments

2

The problem with your first code snippet is that it copies the middle four characters to the beginning of the string, and then stops.

Unfortunately, you cannot expand it to cover the entire string, because in that case the source and output buffers would overlap, causing UB:

If the strings overlap, the behavior is undefined.

Overlapping buffers is the problem with your second attempt: memcpy does not allow overlapping buffers, so the behavior is undefined.

If all you need is to remove characters at the beginning of the string, you do not need to copy it at all: simply take the address of the initial character, and use it as your new string:

char *strWithoutPrefix = &contentSave2[8];

For copying of strings from one buffer to another use memcpy:

char middle[5];
memcpy(middle, &contentSave2[8], 4);
middle[4] = '\0'; // "this"

For copying potentially overlapping buffers use memmove:

char contentSave2[180] = "abcdefg-this-is-a-test";
printf("%s\n", contentSave2);
memmove(contentSave2, contentSave2+8, strlen(contentSave2)-8+1);
printf("%s\n", contentSave2);

Demo.

9 Comments

Can you please explain the first paragraph? I actually failing to see the problem with OPs code.
@EugeneSh. strncpy is often used, incorrectly, with strings of variable length. The biggest problem with that is that you must remember to null-terminate the resulting string in the end. Here is an article explaining what's wrong with using n functions when working with regular strings.
But what is the problem in the question? I don't see it.
@EugeneSh. I edited the answer. The problem with his edited code is that the buffers overlap, so strncpy is no longer applicable.
I would argue that there is no overlap in his code.
|
2

Simply you can use pointer because contentSave2 here is also a pointer to a char array plus this will be quick and short.

char* ptr = contentSave2 + 6;

ptr[0] will be equal to contentSave2[6]

Comments

1

You can use memmove function.

It is specially used when source and destination memory addresses overlap.

Small word of advice, try to avoid copying to and from overlapping source and destination. It is simply a buggen.

Comments

0

The following snippet should works fine:

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

int main() {
    char contentSave2[180] = "abcdefg-this-is-a-test";
    strncpy(contentSave2, contentSave2+8, 4);
    printf("%s\n", contentSave2);
    return 0;
}

I would suggest posting the rest of your code because your issue is elsewhere. As others pointed out, watch out for overlap when you use strncpy though in this specific case it should works.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.