- 5
Can someone explain to me how come the output of the code below is 4?
string s = "SoloLearn"; int x = s.Length; int y = s.IndexOf("e"); Console.Write(x%y);
5 ответов
+ 4
9 mod 5
So, The ans will be 4
+ 2
"SoloLearn" is long 9
C# starts to count from 0 , so "S" is in "0" position the "e" is in fifth position
9 mod 5 = 4
and if you don't know what "modulo operation" is : https://en.wikipedia.org/wiki/Modulo_operation
0
i think, here important thing is, for .Length method starts with 1 , but index of array starts with 0.
0
im still alil confused can you please explain it over alil slower and whats the meaning of MOD @Sam
- 1
Here:
x = 9
y = 5 ( cause "e" is the 5th index from 0, aka "S"
So when x%y is actually saying "what is the remainder from 9 devided by 5, which is 4 because 9/4 is 1 with a remainder of 4, aka 1.4.
I hope that helped.