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