w3resource

C#: Possible Palindromes from string of characters

C# Sharp Regular Expression: Exercise-3 with Solution

From Wikipedia
A palindrome is a word, number, phrase, or other sequence of characters which reads the same backward as forward, such as madam or racecar.

Write a C# Sharp program to check whether a given string of characters can be transformed into a palindrome. Return true otherwise false.

Sample Data:
("amamd") -> True
("pamamd") -> False
("ferre") -> True

Sample Solution-1:

C# Sharp Code:

using System;
using System.Text.RegularExpressions;
using System.Linq;
namespace exercises
{
    class Program
    {
        static void Main(string[] args)
        {

            string text = "amamd";
            Console.WriteLine("Original string: " + text);
            Console.WriteLine("Check the said string of characters can be transformed into a palindrome? " + test(text));
            text = "pamamd";
            Console.WriteLine("\nOriginal string: " + text);
            Console.WriteLine("Check the said string of characters can be transformed into a palindrome? " + test(text));
            text = "ferre";
            Console.WriteLine("\nOriginal string: " + text);
            Console.WriteLine("Check the said string of characters can be transformed into a palindrome? " + test(text));

        }

        public static bool test(string text)
        {
            bool result = Regex.Replace(string.Concat(text.OrderBy(x => x)),@"([a-z])\1{1}",string.Empty).Length <= 1;
            return result;
        }
    }
}

Sample Output:

Original string: amamd
Check the said string of characters can be transformed into a palindrome? True

Original string: pamamd
Check the said string of characters can be transformed into a palindrome? False

Original string: ferre
Check the said string of characters can be transformed into a palindrome? True

Flowchart:

Flowchart: C# Sharp Exercises - Possible Palindromes from string of characters

Sample Solution-2:

C# Sharp Code:

using System;
using System.Text.RegularExpressions;
using System.Linq;
namespace exercises
{
    class Program
    {
        static void Main(string[] args)
        {

            string text = "amamd";
            Console.WriteLine("Original string: " + text);
            Console.WriteLine("Check the said string of characters can be transformed into a palindrome? " + test(text));
            text = "pamamd";
            Console.WriteLine("\nOriginal string: " + text);
            Console.WriteLine("Check the said string of characters can be transformed into a palindrome? " + test(text));
            text = "ferre";
            Console.WriteLine("\nOriginal string: " + text);
            Console.WriteLine("Check the said string of characters can be transformed into a palindrome? " + test(text));

        }

        public static bool test(string text)
        {
            text = string.Concat(text.OrderBy(c => c));
            bool result =  Regex.Replace(text, @"(.)\1", "").Length <= 1;
            return result;
        }
    }
}

Sample Output:

Original string: amamd
Check the said string of characters can be transformed into a palindrome? True

Original string: pamamd
Check the said string of characters can be transformed into a palindrome? False

Original string: ferre
Check the said string of characters can be transformed into a palindrome? True

Flowchart:

Flowchart: C# Sharp Exercises - Possible Palindromes from string of characters

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

Previous C# Sharp Exercise: Calculate the average word length in a string.
Next C# Sharp Exercise: Validate a password.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.




We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook