Kindergarten Counting Game

Everybody sit down in a circle. Ok. Listen to me carefully.

“Woooooo, you scwewy wabbit!”

Now, could someone tell me how many words I just said?

Input and Output

Input to your program will consist of a series of lines, each line containing multiple words (at least one). A “word” is defined as a consecutive sequence of letters (upper and/or lower case).

Your program should output a word count for each line of input. Each word count should be printed on a separate line.

Sample Input

Meep Meep!

I tot I taw a putty tat.

I did! I did! I did taw a putty tat.

Shsssssssssh … I am hunting wabbits. Heh Heh Heh Heh …

Sample Output

2

7

10

9

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Collections;

 

namespace ConsoleApplication3

{

    class Program

    {

        static void Main(string[] args)

        {

            string line = "Shsssssssssh … I am hunting wabbits. Heh Heh Heh Heh …";

            int wordCount = 0;

            int i = 0;

            for (i = 0; i < line.Length – 1; i++)

            {

                if (line[i] != ‘ ‘ && line[i + 1] == ‘ ‘)

                {

                    string tempWord = "";

                    for (int j = i; j >= 0; j–)

                    {

                        if (line[j] != ‘ ‘)

                        {

                            tempWord = tempWord + line[j];

                        }

                        else

                        {

                            break;

                        }

                    }

                    if (ValidWord(tempWord))

                    {

                        wordCount++;

                    }

                }

            }

            if (line[i] != ‘ ‘)

            {

                string tempWord = "";

                for (int j = i; j >= 0; j–)

                {

                    if (line[j] != ‘ ‘)

                    {

                        tempWord = tempWord + line[j];

                    }

                    else

                    {

                        break;

                    }

                }

                if (ValidWord(tempWord))

                {

                    wordCount++;

                }

            }

            Console.WriteLine("Number of words in the line = " + wordCount);

        }

        public static bool ValidWord(string word)

        {

            string upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

            string lower = "abcdefghijklmnopqrstuvwxyz";

            for (int i = 0; i < word.Length; i++)

            {

                if (upper.Contains(word[i]) || lower.Contains(word[i]))

                {

                    return true;

                }

            }

            return false;

        }

    }

}

 

Number of words in the line = 9
Press any key to continue . . .

Advertisements
This entry was posted in Information Technology. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s