Thread: split string and remove substring

  1. #1
    Registered User
    Join Date
    Mar 2009

    Question split string and remove substring

    I want to remove the character '$' and substring "%%&&**" from an input string S. The substring can occur many times within S. I need to use a function that can split S into 2 substring Sleft and Sright, then call this function from main function to remove the substring and concatenate the Sleft and Sright into a new string call S2.

    Please help me!

  2. #2
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    strchr in conjunction with strcat would be a good bet.

    Otherwise, looping through the string on your own wouldn't be that difficult either. Both ways you will need to incorporate a loop.

  3. #3
    Registered User
    Join Date
    Mar 2009
    sorry, I'm a newbie, I understand strchr and strcat, but I can't figure out how to loop the pointer. Here is what I understand so far

    fgets(buf,250,s);                  //read file
         while (!feof(s))

  4. #4
    Registered User
    Join Date
    Sep 2006
    If the substring that you want to remove can occur several times in the string, then you'll need to remove each instance, and concatenate the string back together again, inside the while loop, instead of outside it.

    So in the code sample that was linked to above, in red, you would need a call to remove and re-join the string, after every substring is found.

    I strongly recommend you take this step by step - not all at once. Get a small test string and assign a smaller substring or char you want to remove. Test it block of code, by block of code, for logical accuracy and for proper syntax.

    Except for the re-joining part, I would work in your test program, right from the code sample on the C reference site on that page. The pointer arithmetic is a little tricky. It's the pointer value returned by strchr(), minus the base address (first char's pointer address), of the string, minus one.

    Simple, but subtle, too.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  2. Remove character from string
    By nooksooncau in forum C Programming
    Replies: 11
    Last Post: 06-05-2006, 09:37 AM
  3. Replies: 6
    Last Post: 04-11-2006, 04:52 PM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  5. please help remove blanks from string
    By cjtotheg in forum C Programming
    Replies: 2
    Last Post: 10-24-2001, 12:21 PM