Trying to get substrings for an array of words and to get the substring which meets the palindrome criteria in C#

StackOverflow https://stackoverflow.com/questions/23235045

  •  07-07-2023
  •  | 
  •  

Question

IDE: Microsoft Visual Studio 2010 Platform: C#

Hi, I am trying to get all the sub strings for words one by one and then trying to get the palindrome for all those sub strings. Any suggestion, how can I achieve this?

Was it helpful?

Solution

Assuming that you have a List<string> as collection of words

// A simple method to reverse the string
public static string Reverse( string s )
{
    char[] charArray = s.ToCharArray();
    Array.Reverse( charArray );
    return new string( charArray );
}

Usage:

var words = new List<String>();
var palindromes = words.Select(w => Reverse(w)); // this assumes that all the words are palindrome;

if you need to check for palindrome before selection then try this

var palindromes = words..Where(w => w == Reverse(w)).Select(w => Reverse(w)); // this assumes that all the words are palindrome;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top