Solved

Remove backslash from string in c

Posted on 2015-01-30
2
559 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
[X]
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
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops 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.

690 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