Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Remove backslash from string in c

Posted on 2015-01-30
2
464 Views
Last Modified: 2015-02-14
Hello,
I need c code to remove backslash from a string.
For example, the string is "abcdef\ghijklmn", I need to remove \ from string and get the other characters to another string as "abcdefghijklmn".
Please help with the c code.

Thanks!
0
Comment
Question by:Manoj Dasaraju
2 Comments
 
LVL 45

Assisted Solution

by:Kent Olsen
Kent Olsen earned 100 total points
ID: 40581029
Hi Manoj,

You'll have to do the work yourself.  :(  But it's not a bad task.  Just find the backslash, and copy the string to the right of it back 1 character (over the backslash).  Repeat if you need to remove all of the backslashes.


char *s1 = "abcdef\ghijklmn";
char *s2;
char *p;

  s2 = strdup (s1);   //  Make a copy of the original.
  p = strchr (s2, '\\');
  while (p)
  {
    strcpy (p, p+1);
    p = strchr (s2, '\\');  //  Check for another backslash.
  }

// This leaves the edited string in s2.



Good Luck,
Kent
0
 
LVL 19

Accepted Solution

by:
simon3270 earned 400 total points
ID: 40590851
Alternatively, you can do it in-place with:

        char s[] = "abcdef\\ghij";
        char *p1, *p2;
        p1 = p2 = s;

        printf("Before  %s\n", s);
        while (*p1) {
                if ((*p1) != '\\') {
                        *p2 = *p1;
                        p2++;
                }
                p1++;
        }
        *p2 = 0;
        printf("After  %s\n", s);

Open in new window

I've doubled up the backslashes - a single \ in a string would treat the character after as a special character (e.g. "\t" would be a single tab character).
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

828 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