Skip to content

Instantly share code, notes, and snippets.

@alexdelgado0792
Last active June 11, 2019 14:45
Show Gist options
  • Save alexdelgado0792/adfd21d30c1016418dcf1202b7aa62cd to your computer and use it in GitHub Desktop.
Save alexdelgado0792/adfd21d30c1016418dcf1202b7aa62cd to your computer and use it in GitHub Desktop.
String utilery
using System.Collections.Generic;
using System.Linq;
namespace ConsoleAppDemo
{
public static class StringUtil
{
private const string Space = " ";
public static string WithoutSpaces(this string value)
{
return value.Replace(Space, string.Empty);
}
public static string ReverseWordLinq(this string data)
{
return new string(data.Select((value, index) => new { value, index })
.OrderByDescending(x => x.index)
.Select(x => x.value)
.ToArray());
}
public static string ReverseWord(this string value)
{
return new string(value.Reverse().ToArray());
}
public static bool IsPalindrome(string originalData)
{
var data = originalData.WithoutSpaces();
var reverseValue = data.ReverseWord();
return data == reverseValue;
}
public static dynamic IsPalindrome(IList<string> originalData)
{
return originalData.Select(x => x.WithoutSpaces())
.Select(x => new
{
original = x,
reverse = x.ReverseWord(),
IsPalidrome = (x == x.ReverseWord())
});
}
public static dynamic WordsCount(string value)
{
var result = value.WithoutSpaces().Select(x => new
{
Letter = x.ToString().ToUpper(),
Count = value.Count(p => p == x)
}).Distinct().OrderByDescending(x => x.Count).ThenBy(x => x.Letter);
return new { Word = value, Result = result };
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment