Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

problem with splitting a string into parts for searching

$
0
0
I am trying to create a spam filter for a message entered by the user. It is supposed to check against a list of keywords/phrases in the program. For each phrase/word I add 1 to the count. When I am finished with the message, I am to display the count to the user. I have the basic code. I can put in one phrase or word and it will give me a 1 or 0 depending on if it is found. My problem is, if I have more than one in the user input, I don't know how to split the user message to look for all keys. For example, if I input "be your own boss", then it will give me a score of 1 because it found it and this is correct. If I input "your own boss", it will give me a zero because it isn't in the list. If I input "buy direct satisfaction guaranteed", I should get a score of 2 because "buy direct" and "satisfaction guaranteed" are both in the list, but instead I get 0. I know that I get 0 because it is looking at the entire string "buy direct satisfaction guaranteed" and this string is not in the list. I'm just not sure where to start the whole string splitting problem. Any help, hints, or suggestions is greatly appreciated!

 class Program
    {
        static void Main(string[] args)
        {
            //Declare variables
            string UserInputString;
            int count = 0;

            //Spam Words/Phrases
            string[] stringArray = {"reverses aging", "stop snoring", "free investment", "dig up dirt on friends", "compare rates", "removes wrinkles", 
                                    "free installation", "free grant money", "collect child support", "amazing stuff", "cash bonus", "free preview", 
                                    "no investment", "serious cash", "additional income", "be your own boss", "buy direct", "congratulations", 
                                    "cures baldness", "no selling", "no medical exams", "gift certificate", "online pharmacy", "pennies a day",
                                    "risk free", "satisfaction guaranteed", "score with babes", "take action now", "urgent", "winner"};

            //Get user input
            Console.WriteLine("Please enter the string to be checked for spam:");
            UserInputString = Console.ReadLine();

            //Lower case
            UserInputString = UserInputString.ToLower();

            //foreach word in stringArray 
            for( int i = 0; i < 30; i++)
                if (UserInputString.Equals(stringArray[i]))
                count = count + 1;
                //end if
            //end for

            //Show spam score
            Console.WriteLine("Spam Score: " + count);

            //Pause to see console
            Console.ReadLine();       
        }
    }


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>