コード例 #1
0
        public int GetNumPalSentences()
        {
            int numPalSentences = 0;

            // text split into strings separated by periods
            string[] sentences = text.Split('.');

            foreach (string str in sentences)
            {
                bool        isPal    = true;
                List <char> charList = new List <char>();

                // reduce to strings containing only letters
                for (int i = 0; i < str.Length; i++)
                {
                    if ((str[i] >= 65 && str[i] <= 90) || (str[i] >= 97 && str[i] <= 122))
                    {
                        char curr = str[i];
                        if (str[i] < 97)
                        {
                            curr += (char)32;
                        }
                        charList.Add(curr);
                    }
                }

                CharStr charStr = new CharStr(charList);
                string  cleanStr = charStr.ToString();
                int     length = cleanStr.Length;
                int     j = 0, k = length - 1;

                if (length > 1)
                {
                    while (j <= k)
                    {
                        if (cleanStr[j] != cleanStr[k])
                        {
                            isPal = false;
                        }
                        j++;
                        k--;
                    }
                }
                else
                {
                    isPal = false;
                }

                if (isPal)
                {
                    numPalSentences++;
                }
            }

            return(numPalSentences);
        }
コード例 #2
0
        public void FindUniqueWords()
        {
            int index = 0;  // index of char in paragraph

            while (index < text.Length)
            {
                List <char> charList = new List <char>();

                // add char to list if it is a letter
                while (index < text.Length && ((text[index] >= 65 && text[index] <= 90) || (text[index] >= 97 && text[index] <= 122)))
                {
                    char curr = text[index];
                    if (text[index] < 97)
                    {
                        curr += (char)32;
                    }
                    charList.Add(curr);
                    index++;
                }

                if (charList.Count > 0)
                {
                    CharStr charStr  = new CharStr(charList);
                    string  currWord = charStr.ToString(); // convert char list to string
                    bool    exists   = false;
                    for (int i = 0; i < uniqueWords.Count && uniqueWords.Count > 0; i++)
                    {
                        // instance of existing word
                        if (String.Equals(currWord, uniqueWords[i].GetWord()))
                        {
                            uniqueWords[i].Increment();
                            exists = true;
                            break;
                        }
                    }
                    // unique word
                    if (!exists)
                    {
                        Word newWord = new Word(currWord);
                        uniqueWords.Add(newWord);
                    }
                }
                else
                {
                    index++;
                }
            }
        }