Thread: Simple Removal of string in string via strtok.

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    19

    Simple Removal of string in string via strtok.

    Hello!!!

    I want to remove the string "/op" from my inputString. I've tried to do this with strtok but he replaces more things then just /op for example if my inputstring is '/op lalalala operhoofd' it will not only remove '/op' but also the 'op' from 'operhoofd'. So my output is 'lalalala herhfd'

    Am I using the wrong command or am I just going all wrong.

    The code:

    Code:
            strcat(userinfo, strtok(inputText,"/op"));
    Greetz!
    Vincent
    Last edited by Vincent Wouters; 11-11-2011 at 09:42 AM.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Vincent Wouters View Post
    Am I using the wrong command or am I just going all wrong.
    Well, strtok() is destructive of the input in the sense that it chops your string up by zeroing out the delimiters. Sometimes that is useful, other times it is not.

    Also, the delimiter string argument to strtok is not a string in the sense that "/op" will just match (and erase) "/op"; it will match '/', 'o', and 'p'. Think of it as an array of characters to match.

    To remove a substring, you might want to try something with strstr(). Find the substring you want to remove, then place a null terminator '\0' there and strcat() onto it the remainder from after the eliminated substring.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    void removeSubstr (char *string, char *sub) {
    	char *match;
    	int len = strlen(sub);
    	while ((match = strstr(string, sub))) {
    		*match = '\0';
    		strcat(string, match+len);
    	}
    }
    
    int main(int argc, const char *argv[]) {
    	char test[] = "okay/op 1234 /opdone";
    	removeSubstr(test, "/op");
    	puts(test);
    	return 0;
    }
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    19
    Quote Originally Posted by MK27 View Post
    Well, strtok() is destructive of the input in the sense that it chops your string up by zeroing out the delimiters. Sometimes that is useful, other times it is not.

    Also, the delimiter string argument to strtok is not a string in the sense that "/op" will just match (and erase) "/op"; it will match '/', 'o', and 'p'. Think of it as an array of characters to match.

    To remove a substring, you might want to try something with strstr(). Find the substring you want to remove, then place a null terminator '\0' there and strcat() onto it the remainder from after the eliminated substring.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    void removeSubstr (char *string, char *sub) {
        char *match;
        int len = strlen(sub);
        while ((match = strstr(string, sub))) {
            *match = '\0';
            strcat(string, match+len);
        }
    }
    
    int main(int argc, const char *argv[]) {
        char test[] = "okay/op 1234 /opdone";
        removeSubstr(test, "/op");
        puts(test);
        return 0;
    }
    U Sir are my personal God for one day.

    Thank you!

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    308
    Maybe he can give me code in the thread I just started a few minutes ago too.
    Probably not, your fortunate.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    It struck me later that the code is not very optimal, because it continuously rescans the string. Also, if you feed it this:

    Code:
    char eg[]= "hellolowworld";
    removeSubstr(eg, "low");
    You'd end up with "helorld", which might not be what you want. So a better version is:

    Code:
    void removeSubstr (char *string, char *sub) {
    	char *match = string;
    	int len = strlen(sub);
    	while ((match = strstr(match, sub))) {
    		*match = '\0';
    		strcat(string, match+len);
                    match++;
    	}
    }
    Which does not repeatedly re-scan, and thereby also misses the helorld pitfall.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Nov 2011
    Posts
    19
    Quote Originally Posted by MK27 View Post
    Well, strtok() is destructive of the input in the sense that it chops your string up by zeroing out the delimiters. Sometimes that is useful, other times it is not.

    Also, the delimiter string argument to strtok is not a string in the sense that "/op" will just match (and erase) "/op"; it will match '/', 'o', and 'p'. Think of it as an array of characters to match.

    To remove a substring, you might want to try something with strstr(). Find the substring you want to remove, then place a null terminator '\0' there and strcat() onto it the remainder from after the eliminated substring.

    Do you know how to remove everything before a certain charachter 2? For Example with a string like C:\test.txt how do your remove everything before '\'?

    It's normally done with something like 'Left()'?

    Greetz!

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Vincent Wouters View Post
    Do you know how to remove everything before a certain charachter 2? For Example with a string like C:\test.txt how do your remove everything before '\'?

    It's normally done with something like 'Left()'?

    Greetz!
    Check out strchr() and strrchr(). That and some more stuff with moving pointers/copying/concatenating (like in removeSubstr) will get you there.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with strtok at the end of a string.
    By TheBigH in forum C Programming
    Replies: 2
    Last Post: 07-11-2011, 07:51 PM
  2. loop in the same string using strtok
    By vitvar in forum C Programming
    Replies: 5
    Last Post: 10-27-2009, 07:14 AM
  3. String to int using strtok and strtol
    By jtullo in forum C Programming
    Replies: 2
    Last Post: 05-06-2007, 04:37 PM
  4. Replies: 7
    Last Post: 07-18-2005, 08:43 AM
  5. strtok and string delimiters
    By Leonardo in forum C Programming
    Replies: 1
    Last Post: 05-01-2003, 04:28 PM