I came across this strange loop behaviour in C. Here is the code:
char arr[SIZE];
fgets(arr, SIZE, stdin);
char brr[SIZE];
int i;
for(i=0; arr[i] != '\0'; i++){
if(arr[i] != ' ') brr[i] = (arr[i] + shift - 'a')%26 + 'a';
else brr[i] = ' ';
}
brr[i-1] = '\0';
The code is for Caesar cipher. It takes some text from the user and stores it in arr
, then encodes it by shifting the characters, and this encoded version is put in brr
. So, if user enters 'car' and shift is 2, brr is 'ect'.
The problem is in the last line. Normally, it should be brr[i] = '\0'
, but for some reason the loop will increase I once more after the condition is wrong. I have tried different combinations and it seems like the problem is somehow related to fgets.
Also, the problem is not specific to for loop, while behaves exactly the same.
Edit: Thanks for all the answers. I see now that I should have taken '\n' into account.