Advertisement
Guest User

Untitled

a guest
May 3rd, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.11 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace _08112ACW2
  5. {
  6.     class Program
  7.     {
  8.         static bool palinCheck(string userInput)
  9.         {
  10.             //Inits
  11.             string x;
  12.             bool f;
  13.             int a, z, g;
  14.             char[] punctArray = { ' ', ',', ';', '!', '?', '.' };
  15.  
  16.             x = userInput;
  17.             g = x.Length;
  18.             z = g - 1;
  19.             a = 0;
  20.  
  21.             if (g == 1)
  22.             { /* if the input is only one character it is a palindrome, we don't need to check */
  23.                 f = true; /* this is unneeded, can simply be set to true on initalization */
  24.             }
  25.             else
  26.             {
  27.                 f = true;
  28.                 while (f && a < z)
  29.                 {
  30.                     while (punctArray.Contains<char>(x[a]))
  31.                     { /* skip punctuation on inputs */
  32.                         a++;
  33.                     }
  34.                     while (punctArray.Contains<char>(x[z]))
  35.                     {
  36.                         z--;
  37.                     }
  38.                     if (x[a].ToString().ToUpper() == x[z].ToString().ToUpper())
  39.                     { /* check inputs from both ends are equal */
  40.                         a++;
  41.                         z--;
  42.                     }
  43.                     else
  44.                     {
  45.                         f = false; /* if any are unequal it isn't a palindrome */
  46.                     }
  47.                 }
  48.             }
  49.             //if (!f)
  50.             //{
  51.             //    return false;
  52.             //}
  53.             //else
  54.             //{
  55.             //    return true;
  56.             //}
  57.             return f;
  58.         }
  59.  
  60.         public static void Main()
  61.         {
  62.             string[] testInputs = new string[5]
  63.             {
  64.                 "hello world",
  65.                 "yo yo oy oy",
  66.                 " 23  3!!2",
  67.                 "./\\/ap:",
  68.                 "129hfknjas"
  69.             };
  70.             foreach (string test in testInputs)
  71.             {
  72.                 Console.WriteLine(palinCheck(test));
  73.             }
  74.             Console.ReadLine();
  75.         }
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement