Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nikola-Andreev/57b08eb779ebcb70aaa17658c3e07fa5 to your computer and use it in GitHub Desktop.
Save Nikola-Andreev/57b08eb779ebcb70aaa17658c3e07fa5 to your computer and use it in GitHub Desktop.
8.String Palindromes
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