Quotations marks and strstr

This is a discussion on Quotations marks and strstr within the C Programming forums, part of the General Programming Boards category; Hi. Quick question. I am trying to debug a program and I can't seem to find anything on google. The ...

  1. #1
    Registered User Tommo's Avatar
    Join Date
    Jun 2007
    Location
    Scotland
    Posts
    101

    Quotations marks and strstr

    Hi. Quick question. I am trying to debug a program and I can't seem to find anything on google.

    The question relates to strstr(). I am connecting to a dictionary site using sockets and seeing if the given word is valid, by evaluating the html. Now, I have a pattern variable which contains the pattern I am looking for in the html. The only problem is, the pattern string has quotation marks in it so I have to escape them with '\'. Now I have tried to do strstr( buffer, pattern ) but it always seems to come out NULL, and I think it may be something to do with the quotation marks. Any idea?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Any idea?
    Not a clue. Do you have any sample code that makes answering your question anything but an exercise in futility?
    My best code is written with the delete key.

  3. #3
    Registered User Tommo's Avatar
    Join Date
    Jun 2007
    Location
    Scotland
    Posts
    101
    Here you go. See attached.

    Code:
    int WordisinDictionary( char *buffer, char *pattern ) {
    
    	if( strstr( buffer, pattern ) == NULL ) {
    		return 1;
    	}
    	else {
    		return 0;
    	}	
    
    }
    Let me explain. The pattern variable is "class=\"results\">No" which appears in the html when the word ISN'T valid. So the if/else statement above isn't very intuitive. Another possible explanation for it not working is that the GET request isn't producing the correct html.
    Attached Files Attached Files
    Last edited by Tommo; 10-04-2007 at 02:48 PM.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'm 99.9% sure that strstr doesn't care the least bit about whether your text is quotes or not - why should it treat those special. The only places where quotes make a difference is in computer languages (such as C, PHP, HTML, etc),where they have a meaning (of separating text strings from keywoards, variables and other "programming" bits). All string functions will quite happily take strings that contain any character sequence you can think of.

    --
    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.

  5. #5
    Registered User Tommo's Avatar
    Join Date
    Jun 2007
    Location
    Scotland
    Posts
    101
    So it should work? Hmm, I'll have to look a bit further at my code then.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Why don't you do a partial strstr for "class" or some such, and print the string you get from that.

    --
    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.

  7. #7
    Registered User Tommo's Avatar
    Join Date
    Jun 2007
    Location
    Scotland
    Posts
    101
    Sorry, I'm not sure I know what you mean.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I mean, search for a part of your string "class=\"results\">No" , such as "class". THen check that you are getting the right thing and not missing spaces etc.

    --
    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.

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by Tommo View Post
    So the if/else statement above isn't very intuitive.
    I'll say. (Are you sure you haven't confused yourself?) WordisinDictionary returns 1 if the word is NOT in the dictionary, and returns 0 if it is.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int WordisinDictionary( char *buffer, char *pattern )
    {
       if ( strstr( buffer, pattern ) == NULL )
       {
          return 1;
       }
       else
       {
          return 0;
       }  
    }
    
    int main( int argc, char *argv[] )
    {
       char *text[] = 
       { 
          "<A class=\"results\">No</A>", /* MATCH */
          "<A class=\"results\">Yes</A>" /* NO MATCH*/
       };
       char pattern[25] = "class=\"results\">No";
       size_t i;
       for ( i = 0; i < sizeof text / sizeof *text; ++i )
       {
          int result = WordisinDictionary( text[i], pattern );
          printf("&#37;s is %s the dictionary\n", text[i], result ? "in" : "NOT in" );
       }
       return 0;
    }
    
    /* my output
    <A class="results">No</A> is NOT in the dictionary
    <A class="results">Yes</A> is in the dictionary
    */
    I might just code it similar to this:
    Code:
          int result = strstr( text[i], pattern ) != 0;
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  10. #10
    Registered User Tommo's Avatar
    Join Date
    Jun 2007
    Location
    Scotland
    Posts
    101
    @ Dave_Sinkula: Sorry I seem to have confused you. If the pattern matches then the word was not found, so if strstr( buffer, pattern ) is NULL, then the pattern was not found and so the word does exist. Basically I am saying, if I don't get "not found" then the word exists.

    @matsp: Ah ok. The thing is, I have gone to the site, searched for an invalid word, then looked at the html for the result, so I shouldn't really need to do this.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Tommo View Post
    @ Dave_Sinkula: Sorry I seem to have confused you. If the pattern matches then the word was not found, so if strstr( buffer, pattern ) is NULL, then the pattern was not found and so the word does exist. Basically I am saying, if I don't get "not found" then the word exists.

    @matsp: Ah ok. The thing is, I have gone to the site, searched for an invalid word, then looked at the html for the result, so I shouldn't really need to do this.
    Yes, but it's sometimes easy to make mistakes here, and if you print what you get back from a partial match that is almost certain to match, then you can CAREFULLY check that it's actually as you think it should be. In your applicaiton, not in a web-browser that MAY do something else.

    --
    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.

  12. #12
    Registered User Tommo's Avatar
    Join Date
    Jun 2007
    Location
    Scotland
    Posts
    101
    Thanks matsp.

    I realise why it doesn't work now. The pattern may not appear when I fill my buffer for the first time, say, so wordisindictionary() returns 1 and breaks from the loop, therefore giving me a message saying invalid words are valid.

  13. #13
    Registered User Tommo's Avatar
    Join Date
    Jun 2007
    Location
    Scotland
    Posts
    101
    OK, got it working by changing 'pattern' to "class=\"results\">" and changing the function to:

    Code:
    int WordisinDictionary( char *buffer, char *pattern ) {
    
    	char *ptr;
    	int len;
    	if( ( ptr = strstr( buffer, pattern ) ) != NULL ) {
    		len = strlen( pattern );
    		if( ptr[len] == 'N' && ptr[len + 1] == 'o' ) {
    			return 0;
    		}
    		else {
    			return 1;
    		}
    	}
    	else {
    		return 0;
    	}	
    
    }
    Thanks for the help.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21