Thread: trouble with strstr

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    5

    trouble with strstr

    I'm trying to write a spam checker using strstr, but I'm having trouble getting it to work properly. I want it to increment the counter when it finds a match, then if 2 or more matches are found to report spam. However it is incrementing when any word/phrase other then the compare string is entered. It also doesn't print the "no spam" message regardless of what is entered. Here is what I have got so far:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main (void)
    {
        char input[128];
        int counter;
      
        counter = 0;
    
        fgets (input, sizeof(input), stdin);
    
        if (strstr(input, "free car") != NULL)
            counter = counter +1;
    
         else
            if (strstr(input, "credit") != NULL)
            counter = counter +1;
    
         else
            if (counter = 2)
            printf( "message contains spam\n" );
              else
              printf( "no spam found\n" );    
            
    
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    There is a difference between = and ==. Also, check your logic.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    5
    Ok so I got it to properly report when no spam keywords are found by using
    Code:
    if (counter == 2)
    but I still can't get it to report spam. I changed the printf statements to show the counter totals to try and figure out what its doing. I get nothing when is enter the spam strings and "counter = 68004" when I enter any non spam. I'm not sure what I'm doing wrong.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Code:
    counter = 0;
    fgets (input, sizeof(input), stdin);
    
    
    if (strstr(input, "free car") != NULL)
        counter = counter +1;
    
    else if (strstr(input, "credit") != NULL)
        counter = counter +1;
    
    else if (counter = 2) /* == already corrected */
        printf( "message contains spam\n" );
    
    else
        printf( "no spam found\n" );
    Here is your code from post #1. I restructured it (by changing whitespace - the code is exactly the same) to help you see how the logic is flowing.

    Note that your program contains no loops, so it will run through "main()" once and exit.

    So, if the first condition on (line 5) is true, you increment counter - but then everything else in the program is skipped. That is how an "if-else" chain works. The same with all the other "if()" statements. Do you see that now it in the code I posted?

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    5
    Yes that helps, thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strstr help
    By +Azazel+ in forum C Programming
    Replies: 3
    Last Post: 10-08-2007, 09:51 AM
  2. Help with strstr
    By Joelito in forum C Programming
    Replies: 10
    Last Post: 04-03-2007, 07:42 AM
  3. pch=strstr?
    By reRanger in forum C++ Programming
    Replies: 1
    Last Post: 11-23-2004, 01:33 PM
  4. strstr
    By linuxdude in forum C Programming
    Replies: 2
    Last Post: 05-21-2004, 06:25 PM
  5. strstr()
    By johnny484 in forum C Programming
    Replies: 2
    Last Post: 01-31-2003, 02:40 PM