using System.Text.RegularExpressions;
public static void Main()
Console.WriteLine(addressLookup("42 Wallaby Way","12345"));
Console.WriteLine(addressLookup("123 Some address","LKDSJFLKFJD"));
Console.WriteLine(addressLookup("42 Wallaby Way 12345","12345"));
Console.WriteLine(addressLookup("123 Something","215"));
Console.WriteLine(addressLookup("123 Something","78945-2345"));
Console.WriteLine(addressLookup("123 Something 78945","78945-4512"));
Console.WriteLine(addressLookup("123 Something 78945-4512","78945"));
Console.WriteLine(addressLookup("123 Something 78945-4512","78945-4512"));
Console.WriteLine(addressLookup("",""));
Console.WriteLine(addressLookup("123 Something 78945-4512","78944-4512"));
Console.WriteLine(addressLookup("123 Something 78945","78944-4512"));
Console.WriteLine(addressLookup("78945 Something","78944-4512"));
Console.WriteLine("\n\n----- Different behavior ------");
Console.WriteLine(addressLookup2("123 Something 78945-4512","78944-4512"));
Console.WriteLine(addressLookup2("123 Something 78945","78944-4512"));
Console.WriteLine(addressLookup2("123 Something 78945","7894"));
Console.WriteLine(addressLookup2("78945 Something","78944-4512"));
public static string addressLookup(string addr, string zip) {
string cleanZip = zip.Trim().ToLower();
string firstFiveCharacters = cleanZip.Substring(0, Math.Min(5, cleanZip.Length));
bool hasZipAsEntered = addr.ToLower().Contains(cleanZip);
bool hasZipFirstFive = addr.ToLower().Contains(firstFiveCharacters);
bool goodFormatZip = Regex.Match(cleanZip, @"^\d{5}(?:[-\s]\d{4})?$").Success;
bool addrHasZipAlready = Regex.Match(addr.Trim(), @"\d{5}(?:[-\s]\d{4})?$").Success;
if(hasZipAsEntered || hasZipFirstFive || !goodFormatZip || addrHasZipAlready) {
geocodeAddress = new string[] { addr };
geocodeAddress = new string[] { addr, zip };
return String.Join(" ", geocodeAddress);
public static string addressLookup2(string addr, string zip) {
string cleanZip = zip.Trim().ToLower();
string firstFiveCharacters = cleanZip.Substring(0, Math.Min(5, cleanZip.Length));
bool goodFormatZip = Regex.Match(cleanZip, @"^\d{5}(?:[-\s]\d{4})?$").Success;
addr = Regex.Replace(addr.Trim(), @"\d{5}(?:[-\s]\d{4})?$", cleanZip);
bool hasZipAsEntered = addr.ToLower().Contains(cleanZip);
bool hasZipFirstFive = addr.ToLower().Contains(firstFiveCharacters);
if(hasZipAsEntered || hasZipFirstFive || !goodFormatZip ) {
geocodeAddress = new string[] { addr };
geocodeAddress = new string[] { addr, zip };
return String.Join(" ", geocodeAddress);