Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove first char of string C

Im trying to remove the first char of the string and keep the remainder, my current code doesnt compile and im confused on how to fix it.

My code:

char * newStr (char * charBuffer)
{
    int len = strlen(charBuffer);
    int i = 1;
    char v;
    if(charBuffer[0] == 'A' || charBuffer[0] == 'Q'){
        for(i=1;i<len;i++)
            v = v + charBuffer[i];
    }
    v = v + '\0';
    return v;
}

Gcc: "Warning: return makes pointer from integer without a cast"

Also: "char * newStr (char * charBuffer)" needs to remain the same.

like image 895
Dacto Avatar asked Jul 28 '10 19:07

Dacto


People also ask

How do you delete the first character in a string C?

To remove the first character of a string, we can use the char *str = str + 1 in C. it means the string starts from the index position 1. Similarly, we can also use the memmove() function in C like this.

How do you get rid of the first and last character in a string C?

To remove the first and last character of a string, we can use the following syntax in C. In the example above, we removed the first character of a string by changing the starting point of a string to index position 1, then we removed the last character by setting its value to \0 . In C \0 means the ending of a string.

How can I remove last character from a string in C#?

To remove the last character of a string, we can use the Remove() method by passing the string. Length-1 as an argument to it. Note: In C# strings are the sequence of characters that can be accessed by using its character index, where the first character index is 0 and the last character index is string. Length-1.


2 Answers

Strings don't work like this in C. You're summing up all of the characters in the buffer into the v variable. You can't use + to concatenate. The code you posted has some serious problems which indicate that there's an understanding gap with how to use C.

Try this:

char *newStr (char *charBuffer) {
  int length = strlen(charBuffer);
  char *str;
  if (length <= 1) {
    str = (char *) malloc(1);
    str[0] = '\0';
  } else {
    str = (char *) malloc(length);
    strcpy(str, &charBuffer[1]);
  }
  return str;
}

or this:

char *newStr (char *charBuffer) {
  char *str;
  if (strlen(charBuffer) == 0)
    str = charBuffer;
  else
    str = charBuffer + 1;
  return str;
}

Depending on whether you want to allocate a new string or not. You'll also have to add the code for handling the cases that don't start with 'Q' or 'A'. I didn't include those because I'm not sure exactly what you're trying to do here.

Make sure you do some research into allocating and deallocating memory with malloc and free. These are fundamental functions to be able to use if you're going to be doing C programming.

like image 53
Erick Robertson Avatar answered Sep 19 '22 05:09

Erick Robertson


Well, your description says you want to deal with "strings", but you code deals with char buffers/pointers. The simplest approach to remove the first character for strings would be

const char *newStr(const char *string)
{
    return string+1;
}

but as that doesn't look at all like what your code is doing, you probabaly want something different. For example, if you want to just remove a leading 'A' or 'Q' and then copy the string to a buffer, you want something like

char *newStr(const char *string)
{
    if (string[0] == 'A' || string[0] == 'Q')
        string++;
    return strdup(string);
}
like image 25
Chris Dodd Avatar answered Sep 20 '22 05:09

Chris Dodd