How do I remove the first n characters from a string in c?

C1116

I have a function drop_left() that removes the first n characters from the string. I increment the pointer n spaces so that the string points to the everything after the first n characters. When I return to main, the function did not actually change the string. What am I doing wrong?

int main(int argc, char** argv) {
    char string[]="drop left";
    drop_left(string, 2);
    printf("Drop left: %s\n" , string);
}

void drop_left(char *s, int n){
        s+=n;
}   
R Sahu

When you change s in drop_left, you are changing a local variable. It does not change where the original string points to in the calling function.

One way to deal with it is to change the contents of the string in order see the change in the calling function. You can use something like:

void drop_left(char *s, int n)
{
   char* s2 = s + n;
   while ( *s2 )
   {
      *s = *s2;
      ++s;
      ++s2;
   }
   *s = '\0';
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I remove multiple characters from a string in C?

how do i remove the first characters of a string [python]

How can I remove from string the four first characters? iMacros

How can I remove the first and last characters from a string?

How do I remove the first or last n characters from a value in q/ kdbstudio?

How to remove first n numeric characters from a string using PHP?

How do I remove \r\n from string in C#?

How do I get the first group of alpha characters from string?

How do I remove some characters from my String

How do I remove characters from a mutable string within a loop?

How do I remove as set of characters from a string in TypeScript

How do I remove the last characters from a string?

How do I remove certain special characters from a string in Lua?

How do I Remove particular dublicate characters from string in javascript?

How to remove first 5 characters from this string?

How do I remove unknown characters in a string?

How do I remove multiple characters in a string

Remove n characters from string c++

How do you remove the first and the last characters from a Multimap's String representation?

how to remove these characters from a string c () \ ][ ""

How to remove all characters after the n first in a string dataframe column?

How do I remove the first characters of a specific column in a table?

How to remove only the first and last characters from a string in Google Sheets

how to remove first and last characters from exploded php string

How to remove first or last few characters from a string?

How do I remove characters from the beginning and end of a string, but not from the middle?

How do I remove a character from a string in c++?

How do I get the first n characters of a string without checking the size or going out of bounds?

How do I remove all non alphanumeric characters from a string except dash?