Thread: How do I remove backslashes from a string

  1. #1
    Registered User
    Join Date
    Oct 2016
    Posts
    3

    How do I remove backslashes from a string

    I need to work with data passed into a c program and the string that is passed in includes backslashes. How can I remove all backslashes for a char array in c?

    In php I would want something like stripslashes();

    This one item is holding up my entire project, any help is greatly appreciated, thanks!

  2. #2
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    For each backslash you are going to have to copy the string a single place to the right so it
    goes over the backslash. This can be achieved using two pointers and a loop.
    Double Helix STL

  3. #3
    Registered User
    Join Date
    Oct 2016
    Posts
    3
    I am not good at the c language. I normally stick to easy stuff like PHP and scripting PowerShell. I wanted to do exactly as you recommend by making a loop that would look at each character then if it matched copy the n+1 item to the n position to overwrite it. However in c after I used the strcpy() function to copy the input into a char array it seems as though the backslash and the following character are now one character instead of there being 2 characters a backslash and a following character.

    For example say I have a char array storing the following data

    char test[10] = "\ab";

    When I go to get the ascii value of the current char in the array it says 7, which is the bell character \a. Therefore when reading the ascii value of the backslash it reads the backslash plus the following character and then returns the ascii value for the special escaped char \a instead of just returning the ascii value of the \ which would be 92.

    I'm not utilizing pointers and maybe that's my issue. Do you have any code examples since I'm not familiar with them?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Err... it sounds like the problem is just that your test strings don't have the backslashes that you want them to contain because they end up denoting escape characters. The solution would be to escape them:
    Code:
    char test[10] = "\\ab";
    Incidentally, this is something that you should have encountered in PHP.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2016
    Posts
    3
    Thanks for the help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Remove problem
    By Non@pp in forum C++ Programming
    Replies: 10
    Last Post: 02-23-2006, 10:31 PM
  2. double backslashes
    By Grunt12 in forum C Programming
    Replies: 4
    Last Post: 12-22-2005, 08:38 AM
  3. How do you use backslashes in include directives?
    By dwks in forum C Programming
    Replies: 9
    Last Post: 10-06-2005, 11:12 AM
  4. remove value from string
    By palku in forum C Programming
    Replies: 4
    Last Post: 10-01-2005, 09:15 PM
  5. How can i add two backslashes to a string?
    By HappyTomato in forum C# Programming
    Replies: 2
    Last Post: 07-28-2005, 03:58 PM

Tags for this Thread