Thread: String search - strstr

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    2

    String search - strstr

    I need help in finding a word in a string

    I need to search for exact work in the string;

    testfld = "FIND THE STRING IN THIS SENTENCE";
    testfld1 = "FIND THESTRING IN THIS SENTENCE";
    testfld2 = "FIND STRING IN THIS SENTENCE THE";

    for ex the output for above should be - searching for "THE"
    for testfld it will be yes
    for testfld1 it will be false
    for testfld2 it will be yes

    strstr will match "THE" in the second string also...i need something which matches exact word in the string (word can be at the start, at the end or anywhere else), but it should match exact word...

    could someone please give me a sample program?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    So what's wrong with starting with strstr(), then adding some additional logic to remove the false matches?

    And no, we're not in the business of handing out code on demand.
    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.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    2
    am sorry i didn't mean to ask for fully working code; just snippets; or even a single line would do;

    back to strstr - if i search for "THE" using strstr it would match "THE", "STRINGTHE", "THESTRING" etc.,

    i want to know if there is a way to find exact word in the sentence??

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Well you find "THE" and then you check the character at either end of it.

    strstr() gives you a pointer, the rest is just a bit of maths, and testing characters.
    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.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    214
    Use strcmp on the strings you found with strstr.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strstr doesn't return FALSE for empty string
    By doia in forum C Programming
    Replies: 3
    Last Post: 07-12-2010, 01:02 PM
  2. strstr string help
    By socket in forum C Programming
    Replies: 7
    Last Post: 10-31-2007, 08:15 AM
  3. partial string matchin strstr
    By bazzano in forum C Programming
    Replies: 3
    Last Post: 10-09-2005, 02:07 PM
  4. Replies: 10
    Last Post: 03-19-2003, 02:15 PM

Tags for this Thread