+ 2
Can anyone solve my error in this code...
Here is my code that is not working.....plzzz give any suggestion https://code.sololearn.com/cGHnIUrkYahP/?ref=app
8 Respuestas
+ 1
class str
{
public static void main(String args[])
{
String s= "She was feeding the little rabbit with an apple";
s=s.toUpperCase();
int i,l,c=0;
char ch,ch1;
l= s.length();
for(i=0;i<l-1;i++)
{
ch= s.charAt(i);
ch1= s.charAt(i+1);
if(ch==ch1)
c++;
}
System.out.println(c);
}
}
+ 2
In line 13 you have
ch1= s.charAt(i+l);
that points to a char outside of the string since l is equal to the string length.
+ 1
You should only loop until l-1, because you are using i+1 inside the loop
As an additional note: don't use l as a variable, since l and I (small l and large i) look the same in some fonts
+ 1
Thats the online line which needs a fix:
instead of
for(i=0;i<l;i++)
use
for(i=0;i<l-1;i++)
+ 1
You also changed line 13 from i+1 to i+l, which was not necessary.
Now it should work
0
Matthias still it is not working
u can do it in code n den resend it to me plzz
0
Matthias thank u soo much but can u xplain me this code by commenting on each line that how it wll work!!