Your code works all fine EXCEPT for 1 category of corner case.
Try changing string length to 101 and see the magic
Here is the test case at str length of 100-
The reason that the error occurs is that the end of string is taken up by position c, so there is nothing to denote end of line or end of string, and hence it continues taking the other “aaaaaa…” as input of string 1.
This results in string 2 being an empty string, and for it the condition is always true.
Its an abnormal behaviour, and hence 1 extra character is always recommended.
(Tested code by asking it to print je…je and got the chars of next line in it. Hence the above theory)