RB-Technical-Test/Anagram/Program.cs

38 lines
1.3 KiB
C#
Raw Normal View History

2020-06-26 12:56:07 +00:00
using System;
namespace Anagram
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(Check("rail safety", "fairy tales")); //Should return true
Console.WriteLine(Check("roast beef", "eat for BSE")); //Should return true (ignores spaces)
Console.WriteLine(Check("DeBiT CaRd", "BAD CREDIT")); //Should return true (ignores case)
Console.WriteLine(Check("empty", "light")); //Should return false (not an anagram)
Console.WriteLine(Check("a", "aa")); //Should return false (not of equal length)
Console.WriteLine(Check("William Shakespeara", "I am a weakish speller")); //Should return false (spelling error)
}
private static bool Check(string s1, string s2)
{
s1 = s1.Replace(" ", String.Empty).ToLower();
s2 = s2.Replace(" ", String.Empty).ToLower();
//If the string lengths aren't equal, they cannot be an anagram.
if (s1.Length != s2.Length)
return false;
while (s1.Length > 0)
{
if (!s2.Contains(s1[0]))
return false;
s2 = s2.Remove(s2.IndexOf(s1[0]), 1);
s1 = s1.Remove(0, 1);
}
return true;
}
}
}