Advertisement
Daniel_007

Palindrome

Aug 26th, 2020 (edited)
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.15 KB | None | 0 0
  1. using System;
  2. using System.Globalization;
  3. using System.Runtime.InteropServices;
  4.  
  5. namespace ConsoleApp2
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             char[] input = Console.ReadLine().ToCharArray();
  12.             Array.Sort(input);
  13.  
  14.             int singleCounter = 0;
  15.             int indexSingle = input.Length-1;
  16.             char before = input[0];
  17.             bool madeCouple = false;
  18.             bool even = false;
  19.             bool successful = false;
  20.             //Even
  21.             if (input.Length % 2 == 0)
  22.             {
  23.                 even = true;
  24.                 for (int i = 1; i < input.Length; i++)
  25.                 {
  26.                     if (madeCouple)
  27.                     {
  28.                         before = input[i];
  29.                         madeCouple = false;
  30.                     }
  31.                     else
  32.                     {
  33.                         char now = input[i];
  34.                         if (now == before)
  35.                         {
  36.                             madeCouple = true;
  37.                         }
  38.                         else
  39.                         {
  40.                             Console.WriteLine("No");
  41.                             return;
  42.                         }
  43.                     }
  44.                 }
  45.                 Console.WriteLine("Yes");
  46.                 successful = true;
  47.             }
  48.             //Odd
  49.             else
  50.             {
  51.                 for (int i = 1; i < input.Length; i++)
  52.                 {
  53.                     if (madeCouple)
  54.                     {
  55.                         before = input[i];
  56.                         madeCouple = false;
  57.                     }
  58.                     else
  59.                     {
  60.                         char now = input[i];
  61.                         if (now == before)
  62.                         {
  63.                             madeCouple = true;
  64.                         }
  65.                         else
  66.                         {
  67.                             before = input[i];
  68.                             indexSingle = i-1;
  69.                             singleCounter++;
  70.                         }
  71.                     }
  72.                 }
  73.  
  74.                 if (singleCounter > 1)
  75.                 {
  76.                     Console.WriteLine("No");
  77.                 }
  78.                 else
  79.                 {
  80.                     Console.WriteLine("Yes");
  81.                     successful = true;
  82.                 }
  83.             }
  84.  
  85.             if (successful)
  86.             {
  87.                 char[] palindrome = new char[input.Length];
  88.                 if (even)
  89.                 {
  90.                     int counter = 0;
  91.                     for (int i = 0; i < palindrome.Length; i++)
  92.                     {
  93.                         if (i % 2 == 0)
  94.                         {
  95.                             palindrome[counter] = input[i];
  96.                             counter++;
  97.                         }
  98.                         else
  99.                         {
  100.                             palindrome[^counter] = input[i];
  101.                         }
  102.                     }
  103.                 }
  104.                 else
  105.                 {
  106.                     int counter = 0;
  107.                     int iCounter = 0;
  108.                     for (int i = 0; i < palindrome.Length; i++)
  109.                     {
  110.                         if (indexSingle == i)
  111.                         {
  112.                             int index = palindrome.Length / 2 ;
  113.                             palindrome[index] = input[i];
  114.                             continue;
  115.                         }
  116.                         if (iCounter % 2 == 0)
  117.                         {
  118.                             palindrome[counter] = input[i];
  119.                             counter++;
  120.                         }
  121.                         else
  122.                         {
  123.                             palindrome[^counter] = input[i];
  124.                         }
  125.  
  126.                         iCounter++;
  127.                     }
  128.                 }
  129.  
  130.                 foreach (var letter in palindrome)
  131.                 {
  132.                     Console.Write(letter);
  133.                 }
  134.             }
  135.         }
  136.     }
  137. }
  138.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement