Informatică, întrebare adresată de freshbiceps, 9 ani în urmă

The following lines of code should test 2 strings for equality. Can you spot a bug? If so, please explain it.
var isEqual = string1.Length == string2.Length;

if (isEqual)
for (var i = 0; i < string1.Length; i++)
isEqual = string1[i] == string2[i];

Console.WriteLine(isEqual);

Răspunsuri la întrebare

Răspuns de CinevaFaraNume
1
In the for loop you should have isEqual = isEqual && string1[i] == string2[i];
Else it wouldn't matter, if the strings are of equal size and the last characters match, the strings are "equal" for this algorithm regardless of the rest of the characters
Alte întrebări interesante