44 lines
1.2 KiB
C#
44 lines
1.2 KiB
C#
namespace First_week
|
|
{
|
|
internal class StringUtils
|
|
{
|
|
public static bool IsPalindrome(string s)
|
|
{
|
|
if (string.IsNullOrEmpty(s))
|
|
return false;
|
|
|
|
string cleaned = new string(s.Where(c => char.IsLetterOrDigit(c)).ToArray()).ToLower();
|
|
string reversed = new string(cleaned.Reverse().ToArray());
|
|
|
|
return cleaned == reversed;
|
|
}
|
|
|
|
public static string ReverseWords(string sentence)
|
|
{
|
|
if (string.IsNullOrEmpty(sentence))
|
|
return sentence;
|
|
|
|
var words = sentence.Split(' ');
|
|
var reversedWords = words.Select(word => new string(word.Reverse().ToArray()));
|
|
|
|
return string.Join(" ", reversedWords);
|
|
}
|
|
|
|
public static string RemovePunctuation(string s)
|
|
{
|
|
if (string.IsNullOrEmpty(s))
|
|
return s;
|
|
|
|
return new string(s.Where(c => !char.IsPunctuation(c)).ToArray());
|
|
}
|
|
|
|
public static bool IsAlphaNumeric(string s)
|
|
{
|
|
if (string.IsNullOrEmpty(s))
|
|
return false;
|
|
|
|
return s.All(char.IsLetterOrDigit);
|
|
}
|
|
}
|
|
}
|