При чтении массив наоборот содержание не должно изменяется - C#
Формулировка задачи:
В этой строке "if (a[i] == a[n - i])" выбивает ошибку.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Number9._3
{
class Program
{
static void Main(string[] args)
{
int i;
const int n = 5;
char[] a = new char[n];
Console.WriteLine("Введите массив из 5 элементов:");
for (i = 0; i < n; i++) a[i] = char.Parse(Console.ReadLine());
for (i = 0; i < n; i++)
if (a[i] == a[n - i])
Console.WriteLine("Слово является палиндромом");
Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
}
}
}Решение задачи: «При чтении массив наоборот содержание не должно изменяется»
textual
Листинг программы
using System;
namespace NS1
{
class Program
{
static void Main(string[] args)
{
int i;
const int n = 5;
char[] a = new char[n];
Console.WriteLine("Введите массив из 5 элементов:");
for (i = 0; i < n; i++) a[i] = char.Parse(Console.ReadLine());
Console.WriteLine(IsPoly(a) ? "Слово является палиндромом" : "Слово не является палиндромом");
Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
}
static bool IsPoly(char[] array)
{
for (int i = 0; i < array.Length % 2; i++)
{
if (array[i] != array[array.Length - 1 - i])
return false;
}
return true;
}
}
}