Created
June 6, 2016 08:42
-
-
Save Nikola-Andreev/57b08eb779ebcb70aaa17658c3e07fa5 to your computer and use it in GitHub Desktop.
8.String Palindromes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Numerics; | |
using System.Globalization; | |
using System.Text.RegularExpressions; | |
namespace Praktice | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
char[] separators = { ' ', ',', '.', '?', '!' }; | |
string[] input = Console.ReadLine().Split(separators,StringSplitOptions.RemoveEmptyEntries); | |
List<string> output = new List<string>(); | |
for (int i = 0; i < input.Length; i++) | |
{ | |
string word = input[i]; | |
bool palindrome = IsPalindrome(word); | |
if (palindrome) | |
{ | |
if (!output.Contains(word)) | |
{ | |
output.Add(word); | |
} | |
} | |
} | |
output.Sort(); | |
Console.WriteLine(string.Join(", ",output)); | |
} | |
private static bool IsPalindrome(string word) | |
{ | |
int min = 0; | |
int max = word.Length - 1; | |
while (true) | |
{ | |
if (min > max) | |
{ | |
return true; | |
} | |
char a = word[min]; | |
char b = word[max]; | |
if (a != b) | |
{ | |
return false; | |
} | |
min++; | |
max--; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment