Advertisement
nikolay_radkov

ExtractPalindroms

Aug 11th, 2013
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.76 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. /*
  6.     Write a program that extracts from a given text all palindromes, e.g. "ABBA", "lamal", "exe".
  7. */
  8.  
  9. class ExtractPalindroms
  10. {
  11.     public static void CheckWord(string word)
  12.     {
  13.         bool isPalindrom = true;
  14.  
  15.         string checkedString = RemoveWitheSpaces(word).ToLower();
  16.  
  17.         for (int i = 0; i < checkedString.Length / 2; i++)
  18.         {
  19.             if (checkedString[i] != checkedString[checkedString.Length - i - 1])
  20.             {
  21.                 isPalindrom = false;
  22.                 break;
  23.             }
  24.         }
  25.         if (isPalindrom)
  26.         {
  27.             Console.WriteLine("{0}\n", word.Trim());      
  28.         }
  29.     }
  30.  
  31.     public static string RemoveWitheSpaces(string words)
  32.     {
  33.         StringBuilder word = new StringBuilder();
  34.  
  35.         for (int i = 0; i < words.Length; i++)
  36.         {
  37.             if (words[i] != ' ')
  38.             {
  39.                 word.Append(words[i]);
  40.             }
  41.         }
  42.  
  43.         return word.ToString();
  44.     }
  45.  
  46.     static void Main(string[] args)
  47.     {
  48.         Console.Title = "Extract palindroms from a text";
  49.  
  50.         //Console.ForegroundColor = ConsoleColor.Cyan;
  51.        // Console.Write("Enter a text: ");
  52.         //string text = Console.ReadLine();
  53.  
  54.         string text = @"Ядох. Е, щом си пял, ако има нужда, мини да ги видиш у дома — саксиен демон ръкомаха мокър, но мед не иска — само души диви гадини... Маджуна ми окаля писмо. Ще ходя!";
  55.  
  56.         string[] separator = { ".", ",", "!", " ", "?", ":", ";", "—", "-", "\"", "'" };
  57.  
  58.         string[] separatedWords = text.Split(separator, StringSplitOptions.RemoveEmptyEntries);
  59.  
  60.  
  61.         List<string> words = new List<string>();
  62.  
  63.         for (int i = 0; i < separatedWords.Length; i++)
  64.         {
  65.             StringBuilder appender = new StringBuilder();
  66.             for (int j = i; j < separatedWords.Length; j++)
  67.             {
  68.                 appender.Append(separatedWords[j]);
  69.                 appender.Append(" ");
  70.                 words.Add(appender.ToString());
  71.             }
  72.         }
  73.  
  74.         Console.ForegroundColor = ConsoleColor.Magenta;
  75.         Console.WriteLine("\nThe palindroms in the text are:");
  76.         Console.Write(new string('-', Console.WindowWidth));
  77.  
  78.         Console.ForegroundColor = ConsoleColor.Yellow;
  79.         for (int i = 0; i < words.Count; i++)
  80.         {
  81.             CheckWord(words[i]);      
  82.         }
  83.        
  84.         Console.ForegroundColor = ConsoleColor.Magenta;
  85.         Console.Write(new string('-', Console.WindowWidth));
  86.  
  87.         Console.WriteLine();
  88.         Console.ResetColor();
  89.  
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement