Click here to Skip to main content
15,905,504 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
so i tried below code but every time the sum value showing zero only
C#
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;
                  }
          }
Posted

I don't I tried your code as written, and it gave me positive numbers:
C#
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:
C#
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.
 
Share this answer
 
v2
Comments
Vishal_Prince 6-Apr-13 7:01am    
But me trying to compare each word of string.which is already entered in 2 textboxes.for Example if 1st textbox text is "ABC" and 2nd textbox text is "XYZ".
i stored both the string in Yname[] and Pname[].
now i want to compare each word of the string. I want to return values. and later the values will store in variable.
but i used your
string[] Yname = {txtUrNa.Text};
string[] Pname = {txtPrNa.Text};

for (i = 0; Yname[i] != null; i++)
{

for (j = 0; Pname[j] != null; j++)
if (String.Equals(Yname[i], Pname[j], StringComparison.OrdinalIgnoreCase) == true)
{
Sum = Sum + 1;
}
}

textBox1.Text = Sum.ToString();

and an error was coming "Index was outside the bounds of the array."at that for loop
OriginalGriff 6-Apr-13 7:22am    
That's because I added them to load a couple of string arrays for testing... :sigh:
This may be you using the wrong terminology: if you are loading Yname and PName from a text box, then Yname and Pnaem are both string objects, which means that Yname[index] and Pname[index] are not words: they are characters. And there will never be a condition where either of them will be null, as a TextBox control never inserts a null into a string - so your loops should never end...

Lets go back a bit and try to work out what you are trying to do. Why are you comparing the textbox contents? What do you want to know, and exactly what do you expect the user to type in them?
Vishal_Prince 6-Apr-13 7:38am    
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.
OriginalGriff 6-Apr-13 8:25am    
Answer updated
Vishal_Prince 6-Apr-13 8:44am    
But still not getting what i want.
every time i am giving same name,but every time the results are different.
try this code
C#
string[] Yname = { "hello", "there", "Paul" , null};
string[] Pname = { "goodbye", "there", "paul" , null};
int Sum = 0;
int i, j;
foreach (string name in Yname)
{
      if (Pname.Contains(name))
      {
          Sum = Sum + 1; 
      }
}


updated solution as per ur comment
C#
string[] Yname = txtUrNa.Text.ToString().Split(',');
string[] Pname = txtPrNa.Text.ToString().Split(',');
int Sum = 0;
foreach (string name in Yname)
{
     if (Pname.Contains(name))
     {
        Sum = Sum + 1;
     }
}
 
Share this answer
 
v2
Comments
Vishal_Prince 6-Apr-13 8:35am    
thanks.I used this code too, But still not getting what i want.
every time i am giving same name,but every time the results are different.
Pallavi Waikar 6-Apr-13 8:58am    
give one example of value u adding in textbox
Vishal_Prince 6-Apr-13 9:01am    
me not giving values,Names of peoples like "Radha","Ram",etc
Pallavi Waikar 6-Apr-13 9:49am    
check updated solution
Vishal_Prince 7-Apr-13 9:39am    
thanks for the help

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900