C#
5. Ввести два окремих рядки, які містять щонайменше по 4 слова. Виконати такі дії:
• Видалити з другої половини першого рядку всі слова які співпадають з першим словом другого рядку
• Вставити в перший рядок після першого слова довжину другого рядку
• Замінити у першому рядку всі пробіли на знак табуляції
Ответы
Ответ:
Console.WriteLine("Введіть перший рядок");
string? str1 = Console.ReadLine();
Console.WriteLine("Введіть другий рядок");
string? str2 = Console.ReadLine();
string[]? str1Mas = str1?.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
string[]? str2Mas = str2?.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
int i = 0;
while (str1Mas != null && i < str1Mas.Length)
{
if (i >= str1Mas.Length / 2)
if (str1Mas[i] == str2Mas?[0])
{
str1Mas = str1Mas.Where((_, index) => index != i).ToArray();
i = 0;
}
i++;
}
if (str1Mas != null)
{
str1 = string.Join(" ", str1Mas);
str1 = str1.Insert(str1.IndexOf(str1Mas[0], StringComparison.Ordinal) + str1Mas[0].Length, str2Mas?.Length.ToString() ?? string.Empty);
}
str1 = str1?.Replace(" ", "\t");
Console.WriteLine(str1);
Console.ReadKey();
Объяснение: