-
Notifications
You must be signed in to change notification settings - Fork 0
/
cSharpPalindrome.cs
52 lines (51 loc) · 1.83 KB
/
cSharpPalindrome.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using System.Text.RegularExpressions;
internal class Program
{
public static string RemoveSpecialCharacters(string str)
//REGEX Method to remove all special characters from user input
{
return Regex.Replace(str, "[^a-zA-Z_.]+", "", RegexOptions.Compiled);
}
public static string Reverse(string s)
//Method to reverse string
{
char[] charArray = s.ToCharArray();
Array.Reverse(charArray);
return new string(charArray);
}
private static void Main(string[] args)
{
Console.WriteLine("Welcome, enter a word to check if it is a palindrome, type 'exit' to exit.");
//Start of global variables
bool isPalindrome = true;
int count = 1;
//Start of program logic
while (true)
{
//Get user input and convert to lowercase and remove all special characters and spaces (REGEX)
string input = Console.ReadLine();
string lowInput = input.ToLower();
string formattedInput = RemoveSpecialCharacters(lowInput);
string flippedInput = Reverse(formattedInput);
if (input == "quit" || input == "exit")
{
break;
}
if (formattedInput == flippedInput)
{
isPalindrome = true;
Console.WriteLine("Palindrome: " + isPalindrome);
Console.WriteLine("Character Length: " + input.Length);
Console.WriteLine("Number of attempts: " + count);
}
else
{
isPalindrome = false;
Console.WriteLine("Palindrome: " + isPalindrome);
Console.WriteLine("Character Length: " + input.Length);
Console.WriteLine("Number of attempts: " + count);
}
count++;
}
}
}