Solved

Remove backslash from string in c

Posted on 2015-01-30
2
388 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:Kdo
Kdo 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now