remove value from string

This is a discussion on remove value from string within the C Programming forums, part of the General Programming Boards category; Hi all, Suppose If I ve string1="/a/b/c" I want to remove /c from string1. Which function I will use here?? ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    8

    remove value from string

    Hi all,

    Suppose If I ve string1="/a/b/c"
    I want to remove /c from string1.

    Which function I will use here??

    Thanks

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    if the substring you want to remove is at the end of the string, just put a 0 at the beginning of the string you want removed. If the substring is somewhere in the middle, then use strcpy() to move the remainder of the string over the top of the substring you want removed. In both cases the string must be writable, that is, it can't be a string literal.

  3. #3
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    269
    Hi

    You can write your own function for this purpose! But first of all you have to be carefuk about your string initialization. In Linux environment char *string="something" is considered as a string constant and you will get a SIGSEGV if you try to change the string content. On the other hand, while programming in ancient MSDOS (not in windows, I have never tried this in windows!) you don't get such an error!.

    Use strstr() in order to find the sub-string and perform a strcpy(). Reading the man pages may help you...

    http://man.linuxquestions.org/?query...ction=0&type=2
    http://man.linuxquestions.org/?query...ction=0&type=2

    Hope helps...

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Code:
    char string1[] = "/a/b/c";
    char *p = strstr(string1, "/c");
    
    *p = 0;
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    8
    Thanks a lot!!!
    I really appreaciate your suggestion!!

    Palku

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  2. problems with overloaded '+' again
    By Brain Cell in forum C++ Programming
    Replies: 9
    Last Post: 04-14-2005, 05:13 PM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM
  5. please help remove blanks from string
    By cjtotheg in forum C Programming
    Replies: 2
    Last Post: 10-24-2001, 12:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21