'I need a function to delete certain characters from a char array in c++ without using any index

for example: if the user enters : ( 23+22+43)

I want the function to do exactly the following :

for(int i =0; i <strlen(x);i ++)
{
   if (x[i]=='+')
   {
     deletfunc(x[i]);
     deletfunc(x[i+1]);
     cout<<x;
   }
}

so that the output will be (2323)

without using index ----> without knowing the exact number of the char in the array for example I couldn't say deletefunc[3] , I don't know if + is the 3rd or 4rth or 2nd element and so on and the + maybe repeated more than once.

Please if anyone can help I had been trying do this task for 4 days now



Solution 1:[1]

Usually when working with a C style string and the instructor says, "No indexes!" they want you to use a pointer.

Here is one way you could use a pointer

char * p = x; // point p at start of array x
while (*p) // loop until p points to the null terminator - the end of the string
{
    if (*p=='+') // if value at p is +
    {
        deletfunc(p - x); // distance between p and x is index
        if (*(p+1)) // make sure there is a p+1 to erase
        {
            deletfunc(p+1 - x); 
        }
    }
    p++; // advance pointer to next character in array x
}
cout << x; // print revised string

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1