Thread: One Function Required

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    6

    Smile One Function Required

    Code:
    char buffer[MAX_LEN_SINGLE_LINE+2];
    char *buff_ptr, *find_ptr,*find_ptr2,*quit1,*quit2,*find_ptr3,*find_ptr4,*find_ptr5,*find_ptr6,*find_ptr7,*find_ptr8;
    	
    const char text2find1[80] = "NW";
    const char text2repl1[80] = "NWELL";
       	
    const char text2find[80] = "V";
    const char text2repl[80] = "VIA";
        
    const char text2finda[80] = "DM1";
    const char text2repla[80] = "M1DMY";
       
    size_t find_len1 = strlen(text2find1);
    size_t find_len2 = strlen(text2find);
    size_t find_len3 = strlen(text2finda);
    while(fgets(buffer,MAX_LEN_SINGLE_LINE+2,oldf))
    {
    buff_ptr = buffer;
    quit1 = strstr(buff_ptr,"DNW");
    quit2 = strstr(buff_ptr,"DMN2V");
    char *quit3=strstr(buff_ptr,"DMP2V");
    if(quit1==NULL&&quit2==NULL&&quit3==NULL)
    {
    while ((find_ptr = strstr(buff_ptr,text2find1)))
    {
            while(buff_ptr < find_ptr)
                    fputc((int)*buff_ptr++,newf);
            fputs(text2repl1,newf);
            buff_ptr += find_len1;
    }
    while ((find_ptr2 = strstr(buff_ptr,text2find)))
    {
            while(buff_ptr < find_ptr2)
                fputc((int)*buff_ptr++,newf);
            fputs(text2repl,newf);
            buff_ptr += find_len2;		
    }
    while ((find_ptr3 = strstr(buff_ptr,text2finda)))
    {
            while(buff_ptr < find_ptr3)
               fputc((int)*buff_ptr++,newf);
            fputs(text2repla,newf);
            buff_ptr += find_len3;
    }
    }fputs(buff_ptr,newf);
    	
    }
    Hello Guys

    I am a novice and I require you guys to help me to conver this into a general function so that when I put replace("a","b") in the main it is done instead of the repetition of the steps as you see.

    Please help me.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Why don't you try write a function, and then we can comment on it?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    If you wrote that code, certainly you can refactor it into a function.

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Yeah, somehow I doubt he wrote it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Brand new to C need favor
    By dontknowc in forum C Programming
    Replies: 5
    Last Post: 09-21-2007, 10:08 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 09:10 AM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM