// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; public class NameMatcher { private static int Score(string s1, string s2) { string[] w1 = s1.Split(" ".ToCharArray()); string[] w2 = s2.Split(" ".ToCharArray()); int score = 0; for(int i = 0; i < w1.Length; i++) { for(int j = 0; j < w2.Length; j++) { if(w1[i] == w2[j]) { score += 4*w1[i].Length; } else if(w1[i].IndexOf(w2[j])==0) { score += 4*w2[j].Length; } else if(w2[j].IndexOf(w1[i])==0) { score += 4*w1[i].Length; } else { int front = 0; while(front < w1[i].Length && front < w2[j].Length && w1[i][front] == w2[j][front]) { front++; } score += 2*front; int back = 0; while(back < w1[i].Length && back < w2[j].Length && w1[i][w1[i].Length-back-1] == w2[j][w2[j].Length-back-1]) { back++; } score += back; } } } return score; } public static string BestMatch(string s, string[] possible) { int ix = -1; int maxscore = -1; for(int i = 0; i < possible.Length; i++) { int score = Score(s.ToUpper(), possible[i].ToUpper()); if(score > maxscore) { ix = i; maxscore = score; } } if(maxscore > 0) { return possible[ix]; } else { return "(unknown)"; } } } class MainClass { public static void Main(string[] args) { string[] name1 = { "FC Copenhagen", "Århus FC", "Tottenham Hotspur", "FC Liverpool", "Chelsea", "Manchester Utd", "Hertha Berlin", "West Bromwich ", "Hannover", "Kaiserslautern", "Manchester City", "Bor. M Gladbach", "Nürnberg", "Mainz" }; string[] name2 = { "FC CPH", "FC Århus", "Tottenham", "Liverpool", "Chelsea FC", "Man Utd", "Hertha BSC Berlin", "West Brom", "Hannover 96", "1. FC Kaiserslautern", "Man City", "Borussia Mgladbach", "1. FC Nürnberg", "FSV Mainz" }; for(int i = 0; i < name2.Length; i++) { Console.WriteLine(name2[i] + " = " + NameMatcher.BestMatch(name2[i], name1)); } for(int i = 0; i < name1.Length; i++) { Console.WriteLine(name1[i] + " = " + NameMatcher.BestMatch(name1[i], name2)); } } }