Thread: return not returning

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    913

    return not returning

    im working on a function that will remove a string from a string. so if i run

    char tmp[] = "testing a not working idea. not not not";
    removeallstrings(tmp, "not");

    tmp should be changed to "testing a working idea". i can get a function to pass threw once, but it wont return a value so a while loop can stop.

    here what i have

    Code:
    int removestring(char *src, char string[]) {
        char *start = strstr(src, string);
        char *end = NULL;
        
        if(start != NULL) {
            end = &src[(strlen(src) - strlen(start) + strlen(string) + 1)];
        
            *start = '\0';
            strcat(src, end);
        } else 
            return 1;
    
        return 0;
    }
    
    int removeallstrings(char *src, char string[]) {   
        while( removestring(src, string) == 0 )
            ;;
    
        return 0;
    }
    
    int main() {
        char tmp[] = "testing a not working idea. not not not";
        removeallstrings(tmp, "not");
        
        printf("%s\n", tmp);
        
        return 0;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You should be looping while strstr( string, toremove ) is not null.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    i tried if((start = strstr(src, string)) != NULL) { no change. the loop still doesnt end. i have something just like this(in the same format) that works fine.

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    i dont get why it wont work if its at the end. i cut it off by putting null in the first letter of the string and then update the possition by add the length of the string to delete. then i take a back up string and add what was after the word, either more letters or null.

    whats wrong?

  5. #5
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    There is something very unique about trying to remove the last word from your line
    so i guess that means my back up is fine, so that leaves me with the loop

    Code:
    |t|e|s|t|i|n|g| |a| |n|o|t| |w|o|r|k|i|n|g| |i|d|e|a|.| |n|o|t| |n|o|t| |n|o|t|\0|
    |t|e|s|t|i|n|g| |a| |\0|
    |t|e|s|t|i|n|g| |a| |w|o|r|k|i|n|g| |i|d|e|a|.| |n|o|t| |n|o|t| |n|o|t|\0|
    |t|e|s|t|i|n|g| |a| |w|o|r|k|i|n|g| |i|d|e|a|.| |\0|
    |t|e|s|t|i|n|g| |a| |w|o|r|k|i|n|g| |i|d|e|a|.| |n|o|t| |n|o|t|\0|
    |t|e|s|t|i|n|g| |a| |w|o|r|k|i|n|g| |i|d|e|a|.| |\0|
    |t|e|s|t|i|n|g| |a| |w|o|r|k|i|n|g| |i|d|e|a|.| |n|o|t|\0|
    |t|e|s|t|i|n|g| |a| |w|o|r|k|i|n|g| |i|d|e|a|.| |\0|
    |t|e|s|t|i|n|g| |a| |w|o|r|k|i|n|g| |i|d|e|a|.| |\0|\0|
    isnt that what happening. the 2nd to the last line has nothing left so srcat copies my null plus its own. a string cant have 2 nulls? what about strtok?

    yes, im sure this is right in front of me, thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C or C++
    By AcerN30 in forum Game Programming
    Replies: 41
    Last Post: 05-30-2008, 06:57 PM
  2. C++ FTP class won't work
    By lord mazdak in forum C++ Programming
    Replies: 8
    Last Post: 12-18-2005, 07:57 AM
  3. Linking OpenGL in Dev-C++
    By linkofazeroth in forum Game Programming
    Replies: 4
    Last Post: 09-13-2005, 10:17 AM
  4. DirectInput help
    By Muphin in forum Game Programming
    Replies: 2
    Last Post: 09-10-2005, 11:52 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM