0

I need help with a program that im using to verify a user's names. there has to be a certain level of leniency to accept differences like hyphens, spaces and apostrophes. Currently im removing useless characters to compare the strings but names of completely different characters with the same length are being Ok'd. how do i check to see the names are suing similar characters. after the useless ones have been removed and mushed together.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace name_v
{
    class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("enter first name");
        string firstname = Console.ReadLine();
        Console.WriteLine("Enter 2nd name");
        string secondname = Console.ReadLine();

        if (firstname == secondname)
        {
            Console.WriteLine("Names are exactly the same");
            Console.ReadLine();
        }


        else
        {

            Console.WriteLine("press enter to Compare");
            Console.ReadLine();
            int numFirstName = firstname.Length;
            int numSecondName = secondname.Length;
            Console.WriteLine("# in 1st = " + numFirstName);
            Console.WriteLine("# in 2nd = " + numSecondName);
            Console.ReadLine();                                                                                                                                                   

            firstname = firstname.Replace(" ", "").Replace("-", "").Replace("'", "").Replace(".", "").ToUpper();
            secondname = secondname.Replace(" ", "").Replace("-", "").Replace("'", "").Replace(".", "").ToUpper();


            Console.WriteLine("Names to be compared as");
            Console.WriteLine(firstname);
            Console.WriteLine(secondname);
            numFirstName = firstname.Length;
            numSecondName = secondname.Length;
            Console.WriteLine("# in 1st = " + numFirstName);
            Console.WriteLine("# in 2nd = " + numSecondName);
            Console.ReadLine();

            int nameLengthDif = numFirstName - numSecondName;     

            if (firstname == secondname)
            {
                Console.WriteLine("Names are the same");
                Console.ReadLine();
            }
            else
            {
                Console.WriteLine("Names are not the same");
                Console.ReadLine();
                        if (nameLengthDif < 3)
                        {
                            Console.WriteLine("But Close enough");
                            Console.ReadLine();
                        }
                        else
                        {
                            Console.WriteLine("And not Close enough");
                            Console.ReadLine();
                        }
            }



        }

    }


}
}
  • have you tried `String.Compare` ?? – PaulShovan Jun 08 '15 at 16:08
  • @gypsyCoder `String.Compare` is going to check for overall comparison, where as it seems that OP needs a character by character difference, excluding apostrophes, hyphens, etc. – Cameron Jun 08 '15 at 16:17
  • 1
    Might be useful http://stackoverflow.com/questions/83777/are-there-any-fuzzy-search-or-string-similarity-functions-libraries-written-for-c – DavidG Jun 08 '15 at 16:18
  • This looks like homework, so I hesitate to do much more than ask for clarification. I would start with comparing the two strings character-by-character and setting a threshold for how "off" they can be. – Cameron Jun 08 '15 at 16:22
  • 1
    Your `.Replace(" ", "").Replace("-", "").Replace("'", "").Replace(".", "").ToUpper()` probably should be an extension method too, so you can reuse the code and change it one place, if need be. – Icemanind Jun 08 '15 at 16:24
  • Is this whole code really necessary to picture your issue? – PiotrWolkowski Jun 08 '15 at 16:46
  • Its not homework, just something im doing on the side. im looking for a character by character comparison so that Mark Johnson and Will Markuss aren't seen as matches becuase they are the same length but different characters. – user4987024 Jun 08 '15 at 17:41

3 Answers3

2

@TestWell gave a great solution for determining character difference, but I just want to show you a much better way to strip characters than stringing .Replace() over and over and over again:

You can just add/remove chars from this array:

private char[] invalid = new char[] {' ','-','_','.'};
private static string cleanString(string input)
{    
   return new string(input.Where(x => !invalid.Contains(x)).ToArray()).ToLower();
}

usage:

 firstname = cleanString(firstname);

or as an extension method:

namespace CustomExtensions
{
 public static class StringExtension
 {
    private static char[] invalid = new char[] {' ','-','_','.'};
    public static string CleanString(this string y)
    {
      return new string(y.Where(x => !invalid.Contains(x)).ToArray()).ToLower();
    }
 }
}

usage:

firstname = firstname.CleanString();
bill
  • 711
  • 1
  • 8
  • 19
0

How about

private static int GetDifferenceCount(string firstName, string lastName)
{
    int differences;
    string longestString = firstName;

    if (longestString.Length < lastName.Length)
        longestString = lastName;

    for (int i = 0; i < longestString.Length; i++)
    {
        try
        {
            if (firstName.Substring(i, 1) != lastName.Substring(i, 1))
                differences++;
        }
        catch
            differences++;
    }

    return differences;
}

To return number of different characters. You could then say

if (GetDifferenceCount(firstName, lastName) >= 2)
    // Do something

Completing an action if 2 or more characters are different.

TestWell
  • 734
  • 10
  • 19
  • 1
    It may be wise to also convert both strings .ToLower() to prevent capitalized letters from being considered different. 'Mike' should be equal to 'mike' – bill Jun 08 '15 at 16:30
  • 1
    Also, Shouldn't 'if (longestString.Length < lastName)' be lastName.Length? – bill Jun 08 '15 at 16:31
  • I would have included that, but I saw the `ToUpper()` in the original code. But good thing to consider. And yes about your second comment. Thanks for checking me, I'm at work w/o a place test. – TestWell Jun 08 '15 at 16:33
0

try as following :

char[] firstName = firstName.ToCharArray();
char[] lastName = lastName.ToCharArray();
char[] res = firstName.Except(lastName).ToArray();
if(res.Length < 1)
{
  res = lastName.Except(firstName).ToArray();
}
nameLengthDif = res.Length
PaulShovan
  • 2,140
  • 1
  • 13
  • 22