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
anshumaEngineeringAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ikeworkCommented:
look at this:

void StripBackslashes( char *pszDest, const char *pszSource );

int main(int argc, char* argv[])
{
      const char *pszSource = "abc\\def\\gh";
      char pszDestination[ 100 ];

      StripBackslashes( pszDestination, pszSource );

      printf( "source: '%s'\n", pszSource );
      printf( "dest  : '%s'\n", pszDestination );

      return 0;
}

void StripBackslashes( char *pszDest, const char *pszSource )
{
      while( *pszSource )
      {
            if( *pszSource != '\\' )
            {
                  *pszDest++ = *pszSource;
            }
            ++pszSource;
      }
      *pszDest = '\0';
}



hope it helps ;)

good luck, ike

0
ikeworkCommented:
be sure to include this:

#include <stdio.h>
0
anshumaEngineeringAuthor Commented:
I need to keep the string same ok

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

stripslash(x1);

x1="abcdgameasd"
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

CodealotCommented:
This does the trick:

void removeChar( char c, char* s )
{
      char* p = 0;
      while( p = strchr( s, c ) )
            memcpy( p, p+1, strlen(p+1)+1 );
}

int main(int argc, char **argv)
{
  char t[] = ("am\\nbe\\rit&\\nb\\ngy&");
  removeChar( '\\', t );
  printf( t );

  return 0;
}
0
ikeworkCommented:
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;
}

0
xv1700Commented:
Here is a nice little bit of code that will do the stripping out in place. It will perform nice and fast as its just basic pointer manipulation:

    // the string we want to strip
    char s[] = "am\\nbe\\rit&\\nb\\ngy&";

    // a read pointer and a write pointer
    char* readPtr = s;
    char* writePtr = s;

    // loop until we reach the null at the end of the string
    while (*readPtr)
    {
        // if the character is not a backslash we can add it to the string
        if (*readPtr != '\\')
        {
           *writePtr = *readPtr;
           writePtr++;
        }
       
        // move on to process the next character
        readPtr++;
    }
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ikeworkCommented:
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
0
ikeworkCommented:
oh yeah, that's it, i guess xv has the best solution, since its one loop and
no additional copy
0
CodealotCommented:
@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?
0
CodealotCommented:
@xv - sorry about that - wasn't paying attention to 's' - good solution!
0
ikeworkCommented:
@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 ;)
0
xv1700Commented:
Thanks guys, that was my first post - I now feel empowered to move on to the more complex.
0
xv1700Commented:
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';
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.