Пример #1
0
        private static List <StringPosition> GetDuplicatedCharacters(string data)

        {
            // Write your code here
            List <StringPosition> test = new List <StringPosition>(); // Create List
            int pos = 0;                                              // Set Variables
            int i   = 0;

            for (int j = i + 1; j < data.Length; j++)                 // Loop through data parameter
            {
                if (char.IsLetter(data[i]) && char.IsLetter(data[j])) // Check if character is a letter
                {
                    if (data[i] == data[j])                           // check if position @i = position @j (if first letter matches next letter)
                    {
                        pos = i;
                        StringPosition obj = new StringPosition();     //create instance of class
                        obj.DuplicatedPosition = pos + 1;              // assign object values
                        obj.DuplicatedLetter   = (data[i]);            // Add to list
                        test.Add(obj);
                    }
                }
                i++;
            }

            for (int n = 0; n < test.Count; n++)  // For Every value , return output
            {
                StringPosition a = test[n];
                Console.WriteLine("Duplicate Value = {0}, Duplicate Letter = {1}", a.DuplicatedPosition, a.DuplicatedLetter);
            }
            return(test);
        }
Пример #2
0
        public List <StringPosition> DuplicatedCharacters(string data)
        {
            var DupicateList = new List <StringPosition>();

            //loops @lenght -  1 of we dont get an index out of range error
            for (int i = 0; i < data.Length - 1; i++)
            {
                //compares the current char of the string to the one after to see if there is a match
                if (data[i] == data[i + 1])
                {
                    //check to exclude excludes spaces
                    if (data[i] != ' ')
                    {
                        StringPosition obj = new StringPosition();
                        obj.DuplicatedLetter   = data[i];
                        obj.DuplicatedPosition = i + 1;
                        DupicateList.Add(obj);
                    }
                }
            }
            return(DupicateList);
        }