Thread: in str search patt and repl

  1. #1
    Registered User xxxrugby's Avatar
    Join Date
    Jan 2005
    Posts
    178

    in str search patt and repl

    I am trying to make some program that in string "str" looks for substring "patt" a change it with string "repl". And save result in string "res". For example, after execute "search_replace(res,"1456","1","123") result should be "res = "123456"", and after "seacrc_replace(res,"...a...a...a...","a","ab" )" result should be "res = ...ab...ab...ab...".

    Code:
    void search_replace( char* res, char* str, char* patt, char* repl)
    I now for functions strstr(), strcat(), strncat() but I have trouble in placing in!
    OK! I dont now how to start. Really I havent get anywhere to show you code.
    How to create that. Do I need some for loop or no! and dont now! This makes me trouble!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define SIZE 1024; 
       
    void search_replace(char* str, char* patt, char* repl, char *res)
    {
        ...
    }
                   
    int main()
    {
        char str[SIZE];
        char patt[SIZE];
        char repl[SIZE];
        char res[SIZE];
    
        printf("Inster string : \n"); scanf("%s", &str);
        printf("Insert substring that I looks for : \n"); scanf("%s", &patt);
        printf("With what do you wwant me to replace : \n"); scanf("%s", &repl);
        
        search_replace(str, patt, repl, res);
    
        printf("\n%s\n", res);
            
         system("PAUSE");
         return 0;
    }
    hk_mp5kpdw yes yes I see. But that is something that I havet seen. But still dont now how to make it!
    k4z1nh0 yes I see that!
    Last edited by xxxrugby; 03-11-2005 at 05:14 PM.
    Sorry for spelling errors, not English!
    xxxrugby: "All Human Race Will Die From My Hand!"
    xxxrugby: "We are all philosophers, when question is about politics!"

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define SIZE 1024; 
       
    void search_replace(char str[] = "1456", char patt[] = "1", char repl[] = "123", char *res)
    {
        ...
    }
    Are default arguments allowed in C?

    Code:
    int main()
    {
        char str[SIZE];
        char patt[SIZE];
        char repl[SIZE];
        char res[SIZE];
    
        printf("Insert string : \n"); scanf("%s", &str);
        printf("Insert substring that I looks for : \n"); scanf("%s", &patt);
        printf("With what do you want me to replace : \n"); scanf("%s", &repl);
        
        search_replace(str, patt, repl, res);
    
        printf("\n%s\n", res);
            
         system("PAUSE");
         return 0;
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    18
    hi are u right???
    man you have pointers, then use so: scanf("%d",str).
    see you code again below.
    Code:
    char str[SIZE];
        char patt[SIZE];
        char repl[SIZE];
        char res[SIZE];
    
        printf("Inster string : \n"); scanf("%s", &str);
        printf("Insert substring that I looks for : \n"); scanf("%s", &str);
        printf("With what do you wwant me to replace : \n"); scanf("%s", &str);
        
        search_replace(str, patt, repl, res);
    you declared 4 vectors consectives of 1024 lenght.
    in code above you use only one vector. and the others?
    as our friend hk_mp5kpdw say you have to do use the rest of vectors, portant when you type:
    Code:
    printf("Inster string : \n"); scanf("%s", &str);
        printf("Insert substring that I looks for : \n"); scanf("%s", &str);
        printf("With what do you wwant me to replace : \n"); scanf("%s", &str);
    you are rewriting in same address!

    wait to have help u!
    /**************Um abraço*************/

  4. #4
    FOX
    Join Date
    May 2005
    Posts
    188
    Anyone have a better method than the one below?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    static char *search_replace(const char *s, const char *pattern, const char *replace)
    {
            char *needle, *tmp;
            int diff;
            size_t s_len = strlen(s);
            size_t pattern_len = strlen(pattern);
            size_t replace_len = strlen(replace);
     
            if ((tmp = malloc(s_len+1+replace_len-pattern_len)) == NULL)
            {
                    printf("%s\n", "Memory allocation error");
                    return EXIT_FAILURE;
            }
    
            if ((needle = strstr(s, pattern)) != NULL)
            {
                    diff = (int) needle - (int) s;
                    strncpy(tmp, s, diff);
                    tmp[diff] = '\0';
    
                    strcat(tmp, replace);
                    strcat(tmp, &needle[pattern_len]);
    
                    return tmp;
            }
            else
            {
                    free(tmp);
                    return NULL;
            }
    }
    
    int main(void)
    {
            char *s = search_replace("Hello, w00t World!", "w00t ", "");
            printf("%s\n", s);
    
            return 0;
    }
    Last edited by ^xor; 05-31-2005 at 08:06 AM.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Nice bump!

    Code:
    static char *search_replace(const char *s, const char *pattern, const char *replace)
    {
            char *needle;
            int diff;
            size_t s_len = strlen(s);
            size_t pattern_len = strlen(pattern);
            size_t replace_len = strlen(replace);
    
            if ((char *tmp = malloc(s_len+1+replace_len-pattern_len)) == NULL)
            {
                    printf("%s\n", "Memory allocation error");
                    return EXIT_FAILURE;
            }
    
            if ((needle = strstr(s, pattern)) != NULL)
            {
                    diff = (int) needle - (int) s;
                    strncpy(tmp, s, diff);
                    tmp[diff] = '\0';
    
                    strcat(tmp, replace);
                    strcat(tmp, &needle[pattern_len]);
    
                    return tmp;
            }
            else
            {
                    return NULL;
            }
    }
    If there is no pattern match, your above code will return a NULL value to the caller but what happens to the memory allocated to your tmp pointer? You should free that before returning otherwise you have a memory leak; you would end up losing the pointer to that allocated memory and have no way to free it at a later time. Alternately, you could determine if there is a match prior to allocating any memory and proceed accordingly.

    What about strings where there are multiple matches for the pattern? Your solution does not solve this part of the requirement as demonstrated in one of the OP's examples.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    FOX
    Join Date
    May 2005
    Posts
    188
    OK I fixed the memory leak, and some syntax errors that snuck up on me when I edited it here. I thought about replacing all the instances of pattern, but sometimes that's not what you want.

    Quote Originally Posted by hk_mp5kpdw
    Nice bump!
    Was that a bit of sarcasm?
    I know it's lame to bump old threads, but I didn't want to create a new thread when there were already so many existing ones that dealt with this topic.
    Last edited by ^xor; 05-31-2005 at 08:14 AM.

  7. #7
    FOX
    Join Date
    May 2005
    Posts
    188
    Revised version of search_replace, this time replacing all instances of pattern and with memory allocation moved outside the function. Any comments? It looks a bit crazy, so I could really need a better solution.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    static char *search_replace(
                    char *dest,
                    const char *src,
                    size_t size,
                    const char *pattern,
                    const char *replace)
    {
            size_t src_len = strlen(src);
            size_t pattern_len = strlen(pattern);
            size_t replace_len = strlen(replace);
    
            char *needle, *tmp;
            int offset_src = 0; /* Current position in src */
            int offset_tmp = 0; /* Current position in tmp */
            int diff;
    
            if ((tmp = malloc(size)) == NULL)
            {
                    puts("Memory allocation error");
                    return 0;
            }
    
            if ((needle = strstr(&src[offset_src], pattern)) == NULL)
            {
                    printf("Pattern not found\n");
                    return 0;
            }
    
            do
            {
                    if (needle == NULL)
                            break;
                    else
                            printf("Found pattern: \"%s\"\n", pattern);
    
                    diff = (int) needle - (int) &src[offset_src];
                    strncpy(&tmp[offset_tmp], &src[offset_src], diff);
    
                    offset_tmp += diff;
                    offset_src += diff + pattern_len;
    
                    strncpy(&tmp[offset_tmp], replace, replace_len);
                    offset_tmp += replace_len;
            }
            while ((needle = strstr(&src[offset_src], pattern)) != NULL);
    
            strncpy(&tmp[offset_tmp], &src[offset_src], src_len-offset_src);
            offset_tmp += src_len-offset_src;
            strncpy(dest, tmp, offset_tmp);
            dest[offset_tmp] = '\0';
            free(tmp);
    
            return dest;
    }
    
    int main(void)
    {
            char *s = "012345remove_me6789 remove_meabcdefgh";
            char *buf = malloc(100);
            search_replace(buf, s, 100, "remove_me", "");
            printf("%s\n", buf);
    
            return 0;
    }
    Last edited by ^xor; 06-01-2005 at 10:37 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 01-02-2009, 07:24 AM