Can't get error here

please help me to find error
code link:https://ide.codingblocks.com/s/101533

while (i<str.length() && str.charAt(i) == ch )

The only error is that you have to add this first condition (in bold) in the while loop. Because during execution when the while loop breaks, ā€˜iā€™ exceeds str.length()-1 and the index goes out of bound. So it will give runtime error in some cases.

@CRX18Aug0044
hlo mam,
still run time error is coming

I am able to submit the code on hackerblocks and pass all test cases after making the above change. Did you try to submit? If it is not running, try copying the sample input and pasting it in the input box again, or refresh.

@CRX18Aug0044
hlo mam
run time error is coming in all test cases while submitting

The problem is not with the code. You can check the following:

  1. Have you made the required change to the code? Make sure you put these conditions in this order only otherwise it will give runtime error
  1. Have you chosen the correct language for the ide while submitting?

@CRX18Aug0044
why order matters here
while ( i<str.length() && str.charAt(i) == ch )
because and operator will check both the conditions

And operator checks both conditions but order matters here because if (str.charAt(i) == ch) condition is checked first and ā€˜iā€™ and is an out of bound index i.e greater than length of string, then an error will be thrown immediately without the next condition being checked. So, first you have to check if ā€˜iā€™ is a valid index and then check the other condition

@CRX18Aug0044
okay, thanks

Please mark the doubt as resolved if you have no further queries!