how to search for keywords in a character array

This is a discussion on how to search for keywords in a character array within the C Programming forums, part of the General Programming Boards category; Hello! I'm working on a project for a beginner C programming class (and I am very, very much a beginner.) ...

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

    how to search for keywords in a character array

    Hello!

    I'm working on a project for a beginner C programming class (and I am very, very much a beginner.) in which an e-mail is read into a character array and then scanned for each of 30 common keywords found in spam mail. For each occurrence of one of the keywords, I have to add a point to the message’s 'spam score,' and display the spam score at the end.

    I'm stuck on how to search for multiple keywords and keep track of them. Our textbook and teacher never really addressed it.

    The following is what I have coded so far:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
    	// 30 keywords to search for
    	const char *string1 = "won";
    	const char *string2 = "bank";
    	const char *string3 = "gift";
    	const char *string4 = "free";
    	const char *string5 = "cheap";
    	const char *string6 = "vicodin";
    	const char *string7 = "viagra";
    	const char *string8 = "drugs";
    	const char *string9 = "nude";
    	const char *string10 = "hot";
    	const char *string11 = "enlarge";
    	const char *string12 = "enhancement";
    	const char *string13 = "money";
    	const char *string14 = "cash";
    	const char *string15 = "save";
    	const char *string16 = "singles";
    	const char *string17 = "coach";
    	const char *string18 = "casino";
    	const char *string19 = "debtfree";
    	const char *string20 = "xanax";
    	const char *string21 = "pharmacy";
    	const char *string22 = "loans";
    	const char *string23 = "winner";
    	const char *string24 = "oxycodone";
    	const char *string25 = "mortgage";
    	const char *string26 = "congratulations";
    	const char *string27 = "insurance";
    	const char *string28 = "DVDs";
    	const char *string29 = "discount";
    	const char *string30 = "valium";
    	
    	char email[500]; // the array to store the e-mail
    	int spamcount; // counter
    
    	printf("Type in your e-mail: \n");
    	scanf ("%449s", email);
    
    
    // in here is where I'm stuck
    	
    	return 0;
    }
    I came up with this code after seeing a strstr function in our book that was the closest thing I could find to any kind of search. Am I on the right track at all?

    My pseudo-code for what I want to do is essentially:

    Search the e-mail for string1,
    if string1 is found, spamcount++,
    else, break.
    Search the e-mail for string2,
    if string2 is found, spamcount++
    else, break

    And so on until I've searched for all 30, at which point I'll print out my final spamcount.

    I just can't figure out how to code those searching and comparing instructions into C language. Does anyone have an example or tips? I'd really appreciate it!

    (I admit I might be going about this the wrong way entirely. Our teacher mentioned something about functions? But I've never understood functions, so I was trying to find a way to code the program without them. We aren't required to use them.)

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,443
    If you don't understand functions, then find some tutorials on line and practice. They're not too complicated, and very much worth your while. We have one on this site here. You should Google for more tutorials and start practicing using them.

    For your problem, I would use an array of spam words and loop through it to search:
    Code:
    #define N_SPAM_WORDS     30
    char *spam_words[N_SPAM_WORDS] = {
        "won",
        "bank",
        ...
        "valium"
    };
    ...
    read in email_text
    loop through each item in spam_words
        if spam_words[i] found in email_text
            spamcount++;

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    2
    Thank you for the suggestion of putting the search terms in an array, I hadn't been aware I could do that. But are there any examples or tutorials for how to search one array with individual parts of another? The closest I've found is strstr, but now that I'm using an array instead of 30 separate strings, I'm not sure strstr will work.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by JesseReed View Post
    Thank you for the suggestion of putting the search terms in an array, I hadn't been aware I could do that. But are there any examples or tutorials for how to search one array with individual parts of another? The closest I've found is strstr, but now that I'm using an array instead of 30 separate strings, I'm not sure strstr will work.
    Code:
    for (int x = 0; x < MAX_SPAM; x++)
       if (strstr(email,spam[x]))
         spamcount++;
    But don't forget the search is case sensitive... If your spam word is "Viagra" it will fail to find "viagra"...
    Some libraries include an extended function stristr() the i meaning that it's case insensitive.
    Last edited by CommonTater; 04-02-2011 at 08:22 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. PIC16F684 Interfacing with Hitachi 44780
    By JTerry in forum C Programming
    Replies: 36
    Last Post: 12-13-2010, 11:13 AM
  2. Can you help me about tolower() in file
    By nctar in forum C Programming
    Replies: 7
    Last Post: 05-12-2010, 10:04 AM
  3. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM
  4. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 11:40 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

Tags for this Thread


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