0

Problem in a line of code.

//Program to calculate even or odd #include<stdio.h> #include<string.h> int main() { char array[1000001]; long long int i,test,len; scanf("%lld",&test); for(i=0;i<test;i++) { scanf("%s",array); len=strlen(array); if(array[len-1]%2==0) printf("YES\n"); else printf("NO\n"); } } The code above works perfectly fine. My question is that why in "if(array[len-1]%2==0)" array[len-1] is taken rather than taking array[len]?

27th Apr 2018, 7:35 PM
Akash papnai
Akash papnai - avatar
1 Answer
+ 1
Array[len] is out of bounds. Arrays start counting as 0, instead of 1, meaning they have one less indices than the length.
27th Apr 2018, 7:43 PM
Ariela
Ariela - avatar