Advertisement
thegreen9

Polindormes checker

Mar 28th, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.63 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace FirstLab
  8. {
  9.     class Program
  10.     {
  11.         static bool ArePalindromes(String str1, String str2)
  12.         {
  13.             if (str1.Length != str2.Length)
  14.                 return false;
  15.  
  16.             Stack<char> stack1 = StringToCharStack(str1);
  17.             Stack<char> stack2 = StringToCharStack(str2);
  18.             Stack<char> stack3 = new Stack<char>();
  19.  
  20.             while (!stack1.IsEmpty())
  21.                 stack3.Push(stack1.Pop());
  22.  
  23.             while (!stack2.IsEmpty())
  24.             {
  25.                 char tmp1 = stack3.Pop();
  26.                 char tmp2 = stack2.Pop();
  27.                 if (tmp1 != tmp2)
  28.                     return false;
  29.             }
  30.  
  31.             return true;
  32.         }
  33.  
  34.         static Stack<char> StringToCharStack(String str)
  35.         {
  36.             Stack<char> stack = new Stack<char>();
  37.             for (int i = 0; i < str.Length; ++i)
  38.                 stack.Push(str[i]);
  39.             return stack;
  40.         }
  41.            
  42.        
  43.         static void Main(String[] args)
  44.         {
  45.             while (true)
  46.             {
  47.                 Console.Clear();
  48.                 Console.WriteLine("Enter first string");
  49.                 String str1 = Console.ReadLine();
  50.                 Console.WriteLine("Enter second string");
  51.                 String str2 = Console.ReadLine();
  52.                 Console.Write("Strings are palindromes: ");
  53.                 Console.WriteLine(ArePalindromes(str1, str2));
  54.                 Console.ReadKey();
  55.             }
  56.            
  57.          
  58.         }
  59.     }
  60. }
  61. using System;
  62. using System.Collections.Generic;
  63. using System.Linq;
  64. using System.Text;
  65. using System.Threading.Tasks;
  66.  
  67. namespace FirstLab
  68. {
  69.     public class Stack<E>
  70.     {
  71.         private class Node<T>
  72.         {
  73.             public T Item { get; set; }
  74.             public Node<T> Next { get; set; }
  75.  
  76.             public Node(T element, Node<T> next)
  77.             {
  78.                 Item = element;
  79.                 Next = next;
  80.             }
  81.         }
  82.  
  83.         private Node<E> top;
  84.  
  85.         public Stack()
  86.         {
  87.         }
  88.  
  89.         public bool IsEmpty()
  90.         {
  91.             return top == null;
  92.         }
  93.  
  94.         public void Push(E item)
  95.         {
  96.             Node<E> node = new Node<E>(item, top);
  97.             top = node;
  98.         }
  99.  
  100.         public E Pop()
  101.         {
  102.             E temp = top.Item;
  103.             top = top.Next;
  104.             return temp;        
  105.         }
  106.  
  107.         public E Peek()
  108.         {
  109.             return top.Item;
  110.         }
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement