想问一下这个要怎么做

编写一程序求出两个字符串:s[]="ThisisCprogrammingtext"t[]="ThisisatextforCprogramming"包含的最长的相同单词(同一个字母的大小写视为不同的字符)。规定单词全由英文字母组成,单词之间由一个或多个空格分隔。

下面是示例代码实现,望采纳:

using System;

namespace LongestWord
{
    class Program
    {
        static void Main(string[] args)
        {
            // 定义两个字符串
            string s = "This is C programming text";
            string t = "This is a text for C programming";

            // 使用字符串的 Split 方法将字符串分割成单词的数组
            string[] words1 = s.Split(' ');
            string[] words2 = t.Split(' ');

            // 定义最长单词的变量,默认为空字符串
            string longestWord = "";

            // 遍历两个单词数组
            for (int i = 0; i < words1.Length; i++)
            {
                for (int j = 0; j < words2.Length; j++)
                {
                    // 如果当前单词相同,且比之前找到的最长单词长,则更新最长单词
                    if (words1[i] == words2[j] && words1[i].Length > longestWord.Length)
                    {
                        longestWord = words1[i];
                    }
                }
            }

            // 输出最长单词
            Console.WriteLine("The longest common word is: " + longestWord);
        }
    }
}

使用LINQ实现的一种方案,如下:

// 将字符串以空格分割成单词数组
var list1 = "This is C programming text".Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
var list2 = "This is a text for C programming".Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
var result = list1
    .Intersect(list2)                   // 求两个数组的交集
    .OrderByDescending(x => x.Length)   // 将得到的交集结果按单词中的字母个数倒排
    .FirstOrDefault();                  // 取倒排后的第一个就是字母最多的那个单词
Console.WriteLine(result);
Console.ReadKey();

输出结果:

programming