Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c remove the first character of an array

Tags:

c

I have a string:

str1 = "abcabcabc";

How can I remove the first character? I would like the end result to be:

str1 = "bcabcabc";
like image 888
hkvega Avatar asked Apr 19 '11 04:04

hkvega


People also ask

How do I remove the first character from an array?

The shift() method removes the first item of an array. The shift() method changes the original array.

How do you remove the first character of a character array in C++?

Remove the first character from the string using erase() string& erase(size_t pos = 0, size_t len = npos); string& erase(size_t pos = 0, size_t len = npos); string& erase(size_t pos = 0, size_t len = npos);

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

Removing the first and last character 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 do you remove the last character of a string in C?

Every string in C ends with '\0'. So you need do this: int size = strlen(my_str); //Total size of string my_str[size-1] = '\0'; This way, you remove the last char.


6 Answers

If you have a character pointer to a string like:

char *s = "This is my string";

then you can just do s++.

If you have a character array, your best bet may be to have a pointer to that array as well:

char s[] = "This is my string";
char *ps = s;

then you can do ps++ and make sure you use ps rather than s.

If you don't want to have a separate pointer to your array then you can use memmove to copy the data:

memmove (s, s+1, strlen (s+1) + 1); // or just strlen (s)

though none of those will work for an initially empty string so you'll have to check that first. Also keep in mind it's not advisable to attempt modifying string literals in this way (or any way, really) since it's undefined as to whether that's allowed.

Another solution is to simply code up a loop:

for (char *ps = s; *ps != '\0'; ps++)
    *ps = *(ps+1);
*ps = '\0';

This will work for all strings, empty or otherwise.

like image 75
paxdiablo Avatar answered Oct 09 '22 07:10

paxdiablo


Pointer tricks (zero-cost):

char* s = "abcd";
char* substr = s + 1;
// substr == "bcd"

Or:

char s[] = "abcd";
char* substr = s + 1;
// substr == "bcd"

In-place via memmove:

char s[] = "abcd";
char* substr = s + 1;
memmove(s, substr, strlen(substr) + 1);
// s == "bcd"

Notice that we must use char[] rather than char*, which would refer to read-only memory, as described here. Furthermore, one should not use strcpy in-place because the src and dest must not overlap for strcpy.


Into a new string via strcpy:

char* src = "abcd";
char* substr = src + 1;
char dest[strlen(substr) + 1];
strcpy(dest, substr);
// dest == "bcd"

Into a new string via C++'s std::string::substr:

std::string src = "abcd";
std::string dest = src.substr(1);
// dest == "bcd"

Into a new string via C++'s std::copy:

std::string src = "abcd";
std::string dest;
std::copy(src.begin() + 1, src.end(), std::back_inserter(dest));
// dest == "bcd"

There's a couple dozen other ways (particularly when including C++), but I'll stop here. :)

like image 20
Mateen Ulhaq Avatar answered Oct 09 '22 06:10

Mateen Ulhaq


If you really meant to say

char str1 [] = "abcabcabc";

Then the easiest thing is

str1 = &str1[1];

If you want to modify the actual data, then you have to just move everything up one position. You can use a loop for that or memmove(). A recursive function is overkill.

If you really meant C++ and you're using the string object then you can use

str1 = str1.substr(1);
like image 26
Adam Avatar answered Oct 09 '22 06:10

Adam


Here is one way to do it:

int index = 0; //index to cull
memmove( &word[ index ] , &word[ index +1], strlen( word ) - index) ;
like image 45
Shreesh Avatar answered Oct 09 '22 07:10

Shreesh


Well as far as i know if you are worried about memory allocation you have to copy (str1+1) into a new string that you personally allocate memory for, then free the first pointer. The really simple way to do it would be to just increment str1 with str1++; That would make it point one character farther than it used to and give you the desired result with just a line of code.

like image 27
Tincu Gabi Avatar answered Oct 09 '22 07:10

Tincu Gabi


#include <stdio.h>
#include <conio.h>
main(){
   char a[10];
   int i;
   gets(a);

   for (i = 0; a[i] != '\0'; i++) {
      a[i] = a[i + 1];
   }

   printf("\n");
   puts(a);
   getch();
}
like image 41
Monideep Bora Avatar answered Oct 09 '22 06:10

Monideep Bora