I don't I tried your code as written, and it gave me positive numbers:
string[] Yname = { "hello", "there", "Paul" , null};
string[] Pname = { "goodbye", "there", "paul" , null};
int Sum = 0;
int i, j;
for (i = 0; Yname[i] != null; i++)
{
for (j = 0; Pname[j] != null; j++)
if (String.Equals(Yname[i], Pname[j], StringComparison.OrdinalIgnoreCase) == false)
{
Sum = Sum + 1;
}
}
Run this, and Sum was 7
The only way you would get zero, is if the first element of either of your string arrays was null, or if all the strings were the same, of the first element of Pname was null.
What are you trying to achieve? Not "get the code working" but why are you writing this code? (And why are you writing it like that?)
"ok i'll try again.
me developing a love meter application.user enters his and his partner name into textboxes when user clicks the button he get results like "love","marriage" etc
in button click i want to compare each of the single characters."
Ah! Try this:
string Yname = "hello";
string Pname = "goodbye";
int Sum = 0;
int i, j;
Yname = Yname.ToLower();
Pname = Pname.ToLower();
foreach (char y in Yname)
{
foreach (char p in Pname)
{
if (y != p)
{
Sum++;
}
}
}
(The first two lines are just for me to test - you will want your textboxes)
This code does what your first code did, but using characters. So with the two string I show, it gives 32 - because it counts each of the characters in Pname for each of the characters in Yname.
If what you want is a count of the number of characters that are in both strings (and I suspect you do) then reverse the
if
condition from
"!="
to
"=="
and you will get 3 - the 'e', plus the 'o' twice.