Link to home
Start Free TrialLog in
Avatar of Amit
AmitFlag for United States of America

asked on

modifying a string

Hello,
  I am trying to strip all the slashes in the string "am\\nbe\\rit&\\nb\\ngy&" . can somebody send me an algorithm for that
SOLUTION
Avatar of ikework
ikework
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
be sure to include this:

#include <stdio.h>
Avatar of Amit

ASKER

I need to keep the string same ok

x1="\abcd\game\a\s\d"

stripslash(x1);

x1="abcdgameasd"
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
is possible, but too much copy-overhead, you would have to replace the following chars with its
previous one's after each removed char, lots of copy, the more efficient way is to allocate
a buffer, like the above code does and copy the result to the original buffer, it could be done in the
function like this:




#include <stdio.h>
#include <string.h>

void StripBackslashes( char *pszValue );

int main(int argc, char* argv[])
{
      char pszStrip[ 100 ];
      strcpy( pszStrip, "am\\v" );

      StripBackslashes( pszStrip );

      printf( "stripped: '%s'\n", pszStrip );

      return 0;
}

void StripBackslashes( char *const pszStrip )
{
      int intLen = strlen( pszStrip );
      if( !intLen ) return;

      char *pszBuffer = new char[ intLen + 1 ];

      char *pszSource = pszStrip;
      char *pszDest = pszBuffer;
            
      while( *pszSource )
      {
            if( *pszSource != '\\' )
            {
                  *pszDest++ = *pszSource;
            }
            ++pszSource;
      }
      *pszDest = '\0';

      strcpy( pszStrip, pszBuffer );

      delete[] pszBuffer;
}

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Codealot, this looks quite nice, but there are lots of hidden loops in your solution,
strchr loops for each found char from the beginning of the string, lets say 4 slashes are
found, 4 times the string will be looped through, and memcpy copies the whole stuff
4 times, so if you are working with long strings, there's a really big overhead compared
to the one-loop and one-copy solution
oh yeah, that's it, i guess xv has the best solution, since its one loop and
no additional copy
Avatar of Codealot
Codealot

@xv1700: Your solution net's "ngy&" in writePtr and an empty string in readPtr?

@ikework: You won't always know the length of your input string - so char pszStrip[ 100 ] won't always suffice. To solve that you need to allocate on the heap, and take care of cleaning up afterwards, or do dynamic allocation (perf. penalty there too), either way you'd have to do more code. If performance is an issue, than it might be worth it, otherwise keeping it simple might be better?
@xv - sorry about that - wasn't paying attention to 's' - good solution!
@codealot: in xv's solution the readptr points to the end of the string s, because it looped through it,
the 2 pointers are only little helpers, which modify the source string itsself, the string s[] holds the source
and the result too, you need to look at this string, the result is there, that's the big benefit of his/her solution
nothing needs to be copied...
to my solution:
>> To solve that you need to allocate on the heap, and take care of cleaning up afterwards
i did that in my second suggestion, but anyway your right with the allocation/deallocation - performance
problem, but it doesn't worth talking about it, since it's not the best solution, as xv teached us...

btw, performance is always an issue, isn't it ;)
Thanks guys, that was my first post - I now feel empowered to move on to the more complex.
I just realised that I forgot to null terminate the string after shrinking it:

// the string we are going to modify in place
    char s[] = "am\\nbe\\rit&\\nb\\ngy&";

    // temporary pointers for string manipulation
    char* readPtr = s;
    char* writePtr = s;

    // loop round the whole string
    while (*readPtr)
    {
        // if its a backslash then skip it
        // otherwise add it back on to our string
        if (*readPtr != '\\')
        {
           *writePtr = *readPtr;

           // move on our write pointer
           writePtr++;
        }

        // move on to read the next byte
        readPtr++;
    }

    // ensure the string is null terminated as it may have shrunk
    *writePtr = '\0';