Thread: Find and replace function

  1. #31
    Registered User
    Join Date
    Sep 2012
    Posts
    49
    first of all thanks for your help adak I went ahead and renamed remove to remove1. Still no go. Also, I went ahead and did the printf of the source last night and it puts it in in the removeString function. However I cannot find anything wrong with that function. It works fine when I hand something to it from main.. Any ideas on maybe how to rewrite that function or why it might not be working?

  2. #32
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Code:
    for (j = 0; j < i; j++)
        result[j] = source[j];
    These lines copy "source" into "result" up to the specified cut-off point.


    Code:
    while (source[j + remove] != '\0')
    {
        result[j] = source[j + remove];
        j++;
    }
    These lines copy the skipped over section of "source" to the end of "result" - the loop will terminate before the null character is copied.

    Code:
    while (result[k] != '\0')
    {
        source[k] = result[k];
        k++;
    }
    These lines depend upon the null character existing in the "result" array, but the null character was never actually copied into that array.

    You need to add the null character to the "result" array after the first "while()" loop.

    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++;
        }
        result[j] = '\0';  // 'j' was already incremented, so it's already in the
                                  // correct position to place the null character
    
        while (result[k] != '\0')
        {
            source[k] = result[k];
            k++;
        }
    
       source[k] = '\0';
    }

  3. #33
    Registered User
    Join Date
    Sep 2012
    Posts
    49
    Ding ding ding! And the winner is Matticus!!! Thanks for your fresh eyes man. You have solved it! It works perfectly now.

  4. #34
    Registered User
    Join Date
    Sep 2012
    Posts
    49
    Quote Originally Posted by Matticus View Post


    Code:
    while (source[j + remove] != '\0')
    {
        result[j] = source[j + remove];
        j++;
    }
    These lines copy the skipped over section of "source" to the end of "result" - the loop will terminate before the null character is copied.
    Any idea how I could terminate the loop right when I get to the null character but include the null character? Or do I have to just add it in manually like I am doing now and like you suggested?

  5. #35
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    There are ways it can be done. But I'm sleepy, so I have to put coding to rest for the night. It would probably be a good idea to leave that exercise to you, anyhow. There is value in figuring these things out.

    For the record, I didn't glance at your code and see the solution. I opened up a notepad file and, by hand, tracked each element of each array for the duration of each loop (using a simplified test case) until I saw the problem. The program will do what you tell it to do - if it doesn't do what you want it to do, then that means you didn't tell it the correct thing. But the point is that a program is predictable, so it shouldn't be difficult (with a program like this) to step through each line of code and see what's going on. That is a very valuable skill that should be developed. Anyway, g'night.

  6. #36
    Registered User
    Join Date
    Sep 2012
    Posts
    49
    Gotcha thanks for your help.

  7. #37
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Sorry hadn't had much time yesterday.
    Have you already tried your program with other inputs, e.g. change the call to replaceString() in main():
    Code:
    int main (void)
    {
        char str[] = "one time let this work";
        void replaceString (char source[],char s1[], char s2[]);
        
        replaceString(str, "several", "one");
    
        return 0;
    }
    Result:
    Code:
    $ ./test
    several time let this work
    *** stack smashing detected ***: ./test terminated
    ...
    Aborted (core dumped)
    Your logic/algorithm in insertString() is flawed, because it doesn't work when the inserted string is bigger than the removed. In that case you write characters to "source" beyond its array limits.

    Bye, Andreas

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