public static string FirstRepeatedWord(string input)
        {
            HashTable hashTable = new HashTable(50);

            string[] brokenUpString = input.Split(" ");
            string   repeatedWord   = "";

            foreach (string word in brokenUpString)
            {
                if (input == "")
                {
                    return(null);
                }
                else if (hashTable.Contains(word))
                {
                    repeatedWord = word;
                    break;
                }
                else
                {
                    hashTable.Add(word, word);
                    continue;
                }
            }
            return(repeatedWord);
        }
        /// <summary>
        /// check if a word is repeated
        /// </summary>
        /// <param name="words"></param>
        /// <returns>first repeated word, or a msg saying there's no repeated words</returns>
        public string RepeatedWord(string words)
        {
            HashTable ht = new HashTable();

            //strip punctuation from a given string and convert all letters to lowercase
            string pattern        = @"[\p{P}\p{S}]";
            string strippedString = Regex.Replace(words, pattern, "").ToLower();

            //split string on the spaces and put all words into an array
            string[] stringArray = strippedString.Split(" ");

            //loop through each word, hash the words and store them in a hashtable
            for (int i = 0; i < stringArray.Length; i++)
            {
                //first check if the hashtable contains word at the current index
                //if there is a mathch, stop searching
                if (ht.Contains(stringArray[i]))
                {
                    return(stringArray[i]);
                }

                //otherwise add the word at the current index to the table
                ht.Add(stringArray[i], i);
                Console.WriteLine(stringArray[i]);
            }
            return("Found no repeating words");
        }
        /// <summary>
        /// Determines if ther are any duplicate words. If duplicates exist, the
        /// method returns the first duplicate word.
        /// </summary>
        /// <param name="book">The text to check</param>
        /// <returns>The first repeated word, else "No Duplicates"</returns>
        public static string RepeatedWord(string book)
        {
            HashTable words = new HashTable(100);

            char[] charsToTrim = { ',', '.', '?', '!' };
            //Get individual words
            string[] tokens = book.Split(" ");
            //Trim special chars and normalize
            for (int i = 0; i < tokens.Length; i++)
            {
                tokens[i] = tokens[i].Trim(charsToTrim).ToLower();
            }

            //Process tokens
            for (int j = 0; j < tokens.Length; j++)
            {
                //Check if word is already in dictionary, return word
                if (words.Contains(tokens[j]))
                {
                    return(tokens[j]);
                }
                //Else add token to words dictionary
                else
                {
                    if (tokens[j] != "")
                    {
                        words.Add(tokens[j], true);
                    }
                }
            }
            return("No Duplicates");
        }
Пример #4
0
        public static string RepeatedWord(string word)
        {
            string[] words = word.Split(' ');

            HashTable newTable = new HashTable();

            for (int i = 0; i < words.Length; i++)
            {
                string repeated = Regex.Replace(words[i].ToLower(), @"(\p{P})]", "");

                if (newTable.Contains(repeated))
                {
                    return(newTable.Get(repeated).Value);
                }

                newTable.Add(repeated, repeated);
            }
            return("All unique words");
        }
Пример #5
0
        public string RepeatedWordChecker(string input)
        {
            input = input.ToLower();

            string[] inputArray = input.Split(" ");

            HashTable table = new HashTable();

            foreach (string word in inputArray)
            {
                if (table.Contains(word))
                {
                    return(word);
                }
                table.Add(word);
            }

            return(null);
        }
Пример #6
0
        /// <summary>
        /// finds the first word that repeats in a string
        /// </summary>
        /// <param name="sentence"> the inputted string</param>
        /// <returns>  the repeated word in the string</returns>
        public static string FindRepeatedWord(string sentence)
        {
            char[]   charArray = new char[] { ',', '/', '!', ' ', '?' };
            string[] stringArr = sentence.Split(charArray);

            int             v      = 0;
            string          repeat = "Not Found";
            HashTable <int> Table  = new HashTable <int>(sentence.Length * 2);

            foreach (string item in stringArr)
            {
                if (Table.Contains(item.ToUpper()))
                {
                    return(repeat = item.ToLower());
                }
                else
                {
                    Table.Add(item.ToUpper(), v);
                    v++;
                }
            }

            return(repeat);
        }