-2

I passed an array of characters to a function and used a pointer to point at the first element of the array. How do I point through each element of the array and delete the characters I don't want. I am trying not to use brackets and other variables just this pointer and maybe another one.

Thanks.

Andrew
  • 5
  • 5
  • 2
    What do you mean by "delete the characters"? An array is fixed in size, thus you cannot delete entries. You can replace entries, but in no way can you delete entries in an array. If you really and truly want to delete elements, then use a container such as `std::string`. – PaulMcKenzie Nov 23 '16 at 03:20
  • I see. Well if I have a char array with elements aabbccbccd and I want to remove all elements b. I am using a pointer to point at each element of the array.. How do I remove these elements with an array. I am trying not to use brackets. – Andrew Nov 23 '16 at 03:22
  • Possible duplicate of [Erasing elements from a vector](http://stackoverflow.com/questions/347441/erasing-elements-from-a-vector) –  Nov 23 '16 at 03:56
  • If you don't have any concerns regarding the order of elements then, you can simply keep copying the current last element of array at the position of deletion and on doing so reduce the size of array by 1. Of-course after this array size will play an important role whenever you access the array. – sameerkn Nov 23 '16 at 06:08

2 Answers2

0

if you really want to do it in your way, you have to declare a new array of characters and then count the element of the characters that you want to be left on the array by iterating on the array through your pointer, that count will be the size of the new array.

example:

char b[] = "acbbca";
char* p = &b[0];
int oldsize = 6;
int newsize = 0;
for ( int a = 0; a < oldsize; a++ )
{
    if(*p!='b')
        newsize++; // increment the newsize when encountered non-b char
    p++;
}

in the snippet above, you count the number of non-b characters, so it will be the size of the new array.

p = &b[0]; // point again to the first element of the array.
char newone[size]; declare the new array that will hold the result
int ctr = 0;
for ( int a = 0; a < oldsize; a++ )
{
    if(*p!='b')
    {
        newone[ctr] = *p; //store non-b characters
        ctr++;
    }
    p++;
}

in the snippet above, it stores all non-b characters to the new array.

another way is to use std::string.

std::string b = "aabbcc";
b.erase(std::remove(b.begin(),b.end(),'b'),b.end());
Lorence Hernandez
  • 1,189
  • 12
  • 23
  • You don't need all of that code to "remove elements" from an array. You don't need any loops either. The STL algorithm functions such as `std::remove` works with regular arrays. – PaulMcKenzie Nov 23 '16 at 05:32
0

Since arrays cannot be resized, there really is no such thing as "removing elements". To actually remove elements, you would need to use a container such as std::string where elements can actually be removed.

Given this, let's assume you can only use an array, and that "removal" means to move the removed values to the end of the array, and then point to where the removed elements start. The STL algorithm function std::remove can be used to accomplish this:

#include <iostream>
#include <algorithm>

int main()
{
    char letters[] = "aabbccbccd";

    // this will point to the first character of the sequence that is to be
    // removed.
    char *ptrStart = std::remove(std::begin(letters), std::end(letters), 'b');

    *ptrStart = '\0';  // null terminate this position
    std::cout << "The characters after erasing are: " << letters;
}

Output:

The characters after erasing are: aaccccd

Live Example

The std::remove just takes the character you want to remove and places it at the end of the array. The return value of std::remove is the point in the array where the removed elements were placed. Basically the return value points to where the discarded elements start (even though the elements aren't actually discarded).

So if you now write a function to do this, it would probably look like this:

void erase_element(char *ptr, char erasechar)
{
   char *ptrStart = std::remove(ptr, ptr + strlen(ptr), erasechar);
   *ptrStart = '\0';  // null terminate this position
}

Live Example 2

We pass a pointer to the first element, and use the strlen() function to determine how long the string is (the function assumes the string is null-terminated).

PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45