简体   繁体   中英

Searching for words in a string and comparing them

I can't come up with a word search code that would still output the word when the position of the word changes


    class Program
    {

        static void Main(string[] arg)
        {

            Console.OutputEncoding = System.Text.Encoding.Unicode;
            Console.InputEncoding = System.Text.Encoding.Unicode;

            var srd = new MultiSet<Garders>()
            {
                new Garders("ірис, троянда, айстра, півонія, жоржин"),
                new Garders("ірис, троянда, айстра, півонія, жоржин, хризантема, гладіолус"),
                new Garders("ірис, троянда, айстра, півонія, гладіолус")

            };
            MultiSet<Garders>.Enumerator e = srd.GetEnumerator();
            string[] temp = new string[3];
            for (int i = 0; i < temp.Length; i++)
            {
                e.MoveNext();
                temp[i] = e.Current.flower;
            }
            e.Reset();
            while (e.MoveNext())
            {
                
                string[] srt = e.Current.flower.Split();

                foreach (var item in srt)
                {
                    if (temp[0].Contains(item) == temp[1].Contains(item) 
                        && temp[1].Contains(item) == temp[2].Contains(item))
                            Console.WriteLine(item);
                }
                Console.WriteLine();

            }
                
        }

    }
}

My code only outputs the same words if they are in the same positions

You have written very complex logic, it should be very easy if you read the problem statement carefully.

Based on the discussion in a comment, you want to compare two collections whether they have the same items in the same order.

You could use List .

and compare like this.

List<string> ls1 = new List<string>() { "1", "2"};
List<string> ls2 = new List<string>() { "1", "2" };
bool isEqual = ls1.SequenceEqual(ls2);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM