?
Solved

Remove backslash from string in c

Posted on 2015-01-30
2
Medium Priority
?
748 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 46

Assisted Solution

by:Kent Olsen
Kent Olsen earned 400 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 20

Accepted Solution

by:
simon3270 earned 1600 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Suggested Courses

809 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