Thread: Find and replace function

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    49

    Find and replace function

    Hello, I am teaching myself programming (not homework) and I have run into the following problem. Quick note before I post the code: I have tried all three of the functions called by the replaceString function and they all work separately. However when I try to use them all (findString, removeString, insertString) in the replaceString function I end up with a "segmentation error. Core dumped."

    I have been at this for a while and would love some help. Thanks.. here is the code with the problem written on the top in a comment.

    Code:
    /* Using the findString, removeString, and insertString functions from preced-
    ing exercises, write a function called replaceString that takes three character
    string arguments as follows
    replaceString (source, s1, s2);
    and that replaces s1 inside source with the character string s2.The function
    should call the findString function to locate s1 inside source, then call the
    removeString function to remove s1 from source, and finally call the
    insertString function to insert s2 into source at the proper location.
    So, the function call
    replaceString (text, "1", "one");
    replaces the first occurrence of the character string "1" inside the character string
    text, if it exists, with the string "one". Similarly, the function call
    replaceString (text, "*", "");
    has the effect of removing the first asterisk inside the text array because the
    replacement string is the null string.
     */
    
    #include <stdio.h>
    #include <stdbool.h>
    
    int main (void)
    {
        void replaceString (char source[],char s1[], char s2[]);
        
        replaceString("one time let this work", "1", "one");
    
    } 
    
    
    
    void replaceString (char source[],char s1[], char s2[])
    {
        int  findString (const char  source[], const char  s[]);
        void removeString (char source[], int i, int remove);
        void insertString (char source[], char insert[], int p);
     
        
        int i = findString(source, s2);
        int count;
        
        while (s2[count] != '\0')
        count++;
        
        removeString(source, i, count);
        
        insertString(source, s1, i);
        
        printf("%s\n", source);
        
        
    
    }
    
    int  findString (const char  source[], const char  s[])
    {
        int  i, j, foundit = false;
     
    
        for ( i = 0;  source[i] != '\0'  &&  !foundit;  ++i ) {
            foundit = true;
    
            for ( j = 0;  s[j] != '\0' &&  foundit;  ++j )
                if ( source[j + i] != s[j] || source[j + i] == '\0' )
                  foundit = false;
    
            if (foundit)
               return i;
        }
        
    
    
        return -1;
    }
    
    void removeString (char source[], int i, int remove)
    // "i" is where to start removing (from findString) and "remove" is # of characters
    // to remove
    {
        char result[81];
        int j;
        int k = 0;
        
        for (j = 0; j < i; j++)
            result[j] = source[j];
    
        while (source[j + remove] != '\0')
        {
        result[j] = source[j + remove];
        j++;
        }
       
        while (result[k] != '\0')
        {
        source[k] = result[k];
        k++;
        } 
       
       source[k] = '\0';
       
         
    }
    
    void insertString (char source[], char insert[], int p)
    // p is where to start inserting and insert[] is what to insert
    {
        int i = 0;
        int j = 0;
        char temp[81];
        
    // convert source to a temp
        while (source[i] != '\0')
        {
        temp[i] = source[i];
        i++;
        }
        temp[i + 1] = source[i + 1];
        
    //change source to insert p     
        while (insert[j] != '\0')
        {
        source[j + p] = insert[j];
        j++;
        }
        
    // add back characters deleted by insertion by taking from temp and place after insert
        while (temp[p] != '\0')
        {
        source[j + p] = temp[p];
        p++;
        }
        source[j + p] = '\0';
        
    }

  2. #2
    Registered User
    Join Date
    Aug 2012
    Posts
    41
    Your "findString" is not working as expected. It is returning a -ve number. Fix that function.

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    49
    It works perfectly for me.. For example I get an output of "1" for the following code which is correct.

    Code:
    #include <stdio.h>
    #include <stdbool.h>
    
    
    int  findString (const char  source[], const char  s[])
    {
        int  i, j, foundit = false;
    
        // try each character in source 
    
        for ( i = 0;  source[i] != '\0'  &&  !foundit;  ++i ) {
            foundit = true;
    
            // now see if corresponding chars from s match 
    
            for ( j = 0;  s[j] != '\0' &&  foundit;  ++j )
                if ( source[j + i] != s[j] || source[j + i] == '\0' )
                  foundit = false;
    
            if (foundit)
               return i;
        }
    
        return -1;
    }
    
    int main (void)
    {
        int  findString (const char  source[], const char  s[]);
        
        printf("%i\n", findString("ballpark", "all"));
    }

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    49
    and I just copy and pasted it in there.. in fact when I was just doing a find and remove function (without the replace so I could debug it halfway through if there were any problems instead of trying to debug the whole find and replace function) it worked fine there and I didnt change it after that.. hope that makes sense..

  5. #5
    Registered User
    Join Date
    Aug 2012
    Posts
    41
    My apologies. I was wrong.
    When i tried to debug with gdb, segmentation fault occured in removestring().

  6. #6
    Registered User
    Join Date
    Sep 2012
    Posts
    49
    I am not sure why that is not validating for you... When I run the code below for removeString I get an output of "the son" which is correct.

    Code:
    #include <stdio.h>
    #include <stdbool.h>
    
    
    void removeString (char source[], int i, int remove)
    {
        char result[81];
        int j;
        int k = 0;
        
        for (j = 0; j < i; j++)
            result[j] = source[j];
    
        while (source[j + remove] != '\0')
        {
        result[j] = source[j + remove];
        j++;
        }
       
        while (result[k] != '\0')
        {
        source[k] = result[k];
        k++;
        } 
       
       source[k] = '\0';
       
         
    }
    
    int main (void)
    {
        void removeString (char source[], int i, int remove);
        char source[] = "the wrong son";
    
        removeString (source, 4, 6);
        printf("%s\n", source);
     
        return 0;
    }

  7. #7
    Registered User
    Join Date
    Sep 2012
    Posts
    49
    anyone else care to jump in?

  8. #8
    Registered User
    Join Date
    Sep 2012
    Posts
    49
    I cannot figure this out... All of my functions work just fine on their own. I realize I am changing my source array every time I send it to a function but I don't see why that would be an issue..Help!

  9. #9
    Registered User Kolazomai's Avatar
    Join Date
    Aug 2006
    Posts
    12
    Hello!

    It is possible that your array of chars 'result' is not 0-terminated. As far as I know there is no guarantee that a freshly initialized char-array is 0-terminated. Therefore your while-loop sometimes (?!) does not terminate and a segmentation fault occurs as your program writes data in unallocated memory.

    Code:
    void removeString (char source[], int i, int remove)
    // "i" is where to start removing (from findString) and "remove" is # of characters
    // to remove
    {
        char result[81];
        int j;
        int k = 0;
        
        for (j = 0; j < i; j++)
            result[j] = source[j];
    
        while (source[j + remove] != '\0')
        {
        result[j] = source[j + remove];
        j++;
        }
       
        while (result[k] != '\0') // may not terminate!
        {
        source[k] = result[k]; // this may read from / write to unallocated memory
        k++;
        } 
       
       source[k] = '\0';
       
    }
    I recommend initializing your char-array:
    Code:
    char result[81] = { 0 };
    Or use memset:
    Code:
    memset(result, 0, 81);
    Greetings,
    Kolazomai

  10. #10
    Registered User
    Join Date
    Sep 2012
    Posts
    49
    ok let me try that

  11. #11
    Registered User
    Join Date
    Sep 2012
    Posts
    49
    I went ahead and initialized the result function and I still get a segmentation error. :/

    Also the function works fine on its own.. for example as shown in post #6 of this thread I get the correct results when I run the function...

  12. #12
    Registered User Kolazomai's Avatar
    Join Date
    Aug 2006
    Posts
    12
    Hey!

    Compile your program with debug information (parameter '-g3' or '-ggdb'). Then run your program in GDB and once it terminates use
    Code:
    backtrace
    . This will show you the line as well as the sequence of functions which caused your program to crash.

    Greetings,
    Kolazomai

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The bug-a-boo in working with strings are the char's that you can't see: a leading or trailing space, a newline, an end of string char, etc.

    Best thing is to have each function, at the end of it, print out the ascii value of each char in the string:
    Code:
    for(i=0;i<lengthOfString;i++)
       printf("%2d ",array[i]);
    Will show you a clear picture of just what is in your char array, REALLY.

    Be especially mindful of one-off errors. I run into them frequently working with strings.

  14. #14
    Registered User
    Join Date
    Sep 2012
    Posts
    49
    ok definitely.. i will give it a shot

  15. #15
    Registered User
    Join Date
    Sep 2012
    Posts
    49
    OK so I am trying to test it like you said, and the first place I can check for what is in my source string is after running it through the findString function in the replaceString function. I leave out the removeString and the insertString function entirely from the replaceString function. I get a segmentation error when I do this too! So it seems like it messes up from the beginning.. I have no idea how to fix it..

    Here is my code:
    Code:
    #include <stdio.h>
    #include <stdbool.h>
    
    int main (void)
    {
        void replaceString (char source[],char s1[], char s2[]);
        char source[] = "one time let this work";
        char s1[] = "1";
        char s2[] = "one";
        
        replaceString(source, s1, s2);
    
    } 
    
    void replaceString (char source[],char s1[], char s2[])
    {
        int  findString (const char  source[], const char  s[]);
        int i = findString(source, s2);
        int count1;
        int j;
        
        while (source[count1] != '\0')
        count1++;
        
        printf("%i", i);
        
        for(j = 0; j < count1; j++)
            printf("%2d ", source[j]);
        
    }
    
    int  findString (const char  source[], const char  s[])
    {
        int  i, j, foundit = false;
     
    
        for ( i = 0;  source[i] != '\0'  &&  !foundit;  ++i ) {
            foundit = true;
    
            for ( j = 0;  s[j] != '\0' &&  foundit;  ++j )
                if ( source[j + i] != s[j] || source[j + i] == '\0' )
                  foundit = false;
    
            if (foundit)
               return i;
        }
        
    
    
        return -1;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Find and replace
    By yoghesh11 in forum C++ Programming
    Replies: 9
    Last Post: 05-14-2012, 12:54 AM
  2. function to find and replace a part of a string
    By 9988776655 in forum C Programming
    Replies: 2
    Last Post: 02-02-2008, 01:39 AM
  3. please help!...find and replace
    By amy589 in forum C++ Programming
    Replies: 26
    Last Post: 10-13-2006, 06:42 PM
  4. Find and replace
    By BobDole1 in forum C++ Programming
    Replies: 1
    Last Post: 04-12-2003, 10:06 PM
  5. Find and Replace Text Function
    By mart_man00 in forum C Programming
    Replies: 45
    Last Post: 03-13-2003, 10:21 PM