[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

modifying a string

Posted on 2004-10-22
13
Medium Priority
?
284 Views
Last Modified: 2010-04-01
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
0
Comment
Question by:anshuma
  • 6
  • 3
  • 3
  • +1
13 Comments
 
LVL 20

Assisted Solution

by:ikework
ikework earned 400 total points
ID: 12381002
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
 
LVL 20

Expert Comment

by:ikework
ID: 12381024
be sure to include this:

#include <stdio.h>
0
 

Author Comment

by:anshuma
ID: 12381028
I need to keep the string same ok

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

stripslash(x1);

x1="abcdgameasd"
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 

Assisted Solution

by:Codealot
Codealot earned 400 total points
ID: 12381076
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
 
LVL 20

Expert Comment

by:ikework
ID: 12381189
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
 

Accepted Solution

by:
xv1700 earned 400 total points
ID: 12381359
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
 
LVL 20

Expert Comment

by:ikework
ID: 12381389
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
 
LVL 20

Expert Comment

by:ikework
ID: 12381449
oh yeah, that's it, i guess xv has the best solution, since its one loop and
no additional copy
0
 

Expert Comment

by:Codealot
ID: 12381577
@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
 

Expert Comment

by:Codealot
ID: 12381627
@xv - sorry about that - wasn't paying attention to 's' - good solution!
0
 
LVL 20

Expert Comment

by:ikework
ID: 12381711
@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
 

Expert Comment

by:xv1700
ID: 12382630
Thanks guys, that was my first post - I now feel empowered to move on to the more complex.
0
 

Expert Comment

by:xv1700
ID: 12382733
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

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

590 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question