Click here to Skip to main content
15,887,335 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Basically i want to be able to compare elements of arrays and identify if any elements are similar.

The code below works only sometimes, when a word has five letters such as "Hello and Hello" it gives out 7, when the word has 4 letters such as well "Hello and hell" it gives out 6, and it gives out 2 when only 2 are similar.

What I have tried:

if(GuessLenght == WordLenght)
            {
                //Check Each Element

                foreach(char Letter in ActualChar)
                {
                    foreach(char letter in GuessChar)
                    {
                        if(Letter == letter)
                        {
                            LetterGuessed++;
                        }
                        else
                        {
                            
                        }
                    }
                }
            }
Posted
Updated 12-May-20 3:16am

1 solution

Just use for instead of foreach, then compare using the character index:

C#
for (int i = 0; i < WordLenght; i++)
{
    if (ActualChar[i] == GuessChar[i]) LetterGuessed++;
}
 
Share this answer
 
v2
Comments
NotAComputerScienceStudent 12-May-20 9:22am    
But if i compare ehello and hello it says 3 are correct when it should say 5
jimmson 12-May-20 9:25am    
Yes, but I was assuming you would use your condition "if(GuessLenght == WordLenght)" first, what would be FALSE.
It is not difficult to do it way you just suggested, but you need different algorithm - just count number of characters in each word and compare that.
NotAComputerScienceStudent 12-May-20 9:26am    
ok
Maciej Los 12-May-20 9:32am    
Not necessary..., unless the order of letters is not important. An algorithm you suggesting will return 6 for set: {ehello, ollehe}.
;)

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