Thread: search a string...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    22

    search a string...

    Hi all,
    i have a problem.. i want to search a string say "abc" from a file and replace this string with say "xyz" can anyone help me to do this....please gimme some tips or sample codes...
    thanks....

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    easiest way (for not to big files
    read the whole file into buffer
    append 0 at the end
    use strstr to locate the string
    replace it with another string
    write buffer back to file
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    22
    Thanks vart,
    i have already done the same thing.... but i am not able to get how to replace the string..... i have used fgets() and strstr()... can u please gimme a simple code for this...

  4. #4
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    strstr() returns a pointer to a location within the string. To replace it, you'd do a memcpy() or loop through one char at a time from apointer[0] - apointer[strlen() - 1].

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    hope - this will help
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
    	char buffer[]="123abc789";
    	char* p = strstr(buffer,"abc");
    	if(p)
    	{
    		strncpy(p,"456",3);
    	}
    	printf("%s",buffer);
    	return 0;
    
    }
    
    /*
    My output
    123456789
    */
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    vart, shame on you for doing compile's homework!!!

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    22
    Thanks vart,
    and thanks kennedy too....
    kennedy this is not a home work... and i am a professional in electronics and communications... but i am a begginer in "C"... thats y asking help with u people..hope u will make me a good "C" programmer too...

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    22
    Hi friends once again some clarifications....
    in the above program.... if the search string is say "gram" and if that string is not there but if there is a string "program" then it says the string found... how to overcome this. .that is adding a space before and after the string.. ???
    thanks..

  9. #9
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
        char str[] = "This is a test pro gram";
        char search[] = "gram";
        char *p;
        
        p = strstr(str,search);
        
        printf("Serach string -> %s\n",str);
        
        if((*(p-1) == ' ') && (*(p + strlen(search)) == ' ' || *(p + strlen(search)) == '\0'))
            printf("Result : String found\n");
        else
            printf("Result : String not found\n");
        
        getchar();
        return 0;
    }
    
    /* my output
    Serach string -> This is a test program
    Result : String not found
    
    Serach string -> This is a test pro gram
    Result : String found
    */
    NOTE: you will have check for the return value str that is *p;

    Hope this will help

    ssharish2005
    Last edited by ssharish2005; 01-12-2007 at 06:49 AM.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well until you match at the start of a string at least.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User
    Join Date
    Oct 2006
    Posts
    22
    wow thannks ssharish2005,
    thanks a lot.....

  12. #12
    Registered User
    Join Date
    Oct 2006
    Posts
    22
    but one more doubt struck my mind..... say i have a very large text file.... and i want to search a string and replace there.... without making one more copy of the file.. how to do this...
    thanks...

  13. #13
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    You'll likely still have to create a new output file with your modified output. Then delete the old one and rename the new one.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  14. #14
    Registered User
    Join Date
    Oct 2006
    Posts
    22
    oh.. fine.. but why cant we search and replace directly in a file itself ???
    thanks....

  15. #15
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    You can:

    1) If the string you are replacing is the same length.

    2) If the string you are replacing is longer than the one you are inserting you'd have to move all of the remaining chars in the file up to the offset (it'd be a lot of fseek()ing but you _could_ do it).

    3) If the string you are replacing is shorter than the one you are inserting you'd have to move the EOF marker (which is already cleared) and insert the string keeping a short LIFO buffer to advance yourself through the file (again, lots of fseek()ing).

    If you notice, two of the three options above, where doable, aren't the smartest things in the world. Coping a file to memory (assuming that the file will fit into memory) then modifing it and returning it to a newly written file is MUCH easier. Even file-to-file modifications are much prefered.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  4. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM