0
Program
import java.util.*; public class Program { public static void main(String[] args) { Scanner sc=new Scanner(System.in); String st=sc.next(); int a=st.length(); int len=0; int len2=1; for(int i=0;i<=a;i++){ char ch=st.charAt(i); len++; if (ch=='.'||ch=='@'||ch==')'||ch=='('||ch=='/'||ch=='-'||ch=='!'||ch=='
#x27;||ch=='#'||ch==':'||ch==';'||ch=='?'||ch=='"'){ len--; } if (ch==' '){ len2++; } } System.out.println ("Length="+len); System.out.println ("Length2="+len2); } } Please explain the error in this code4 ответов
+ 1
Amran
Index starts from 0 so if length is 5 then iteration will work till < not <=
if you use <= it means you are accessing character at index 5 but as index starts from 0 so you need to access value at (5 - 1) = 4th index
+ 2
Thank you sir for explaining
+ 1
i < a;
0
Thank you.Can you please explain