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 code

21st Jan 2023, 12:40 PM
Amran
Amran - avatar
4 Respostas
+ 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
21st Jan 2023, 12:50 PM
A͢J
A͢J - avatar
+ 2
Thank you sir for explaining
21st Jan 2023, 12:51 PM
Amran
Amran - avatar
+ 1
i < a;
21st Jan 2023, 12:42 PM
A͢J
A͢J - avatar
0
Thank you.Can you please explain
21st Jan 2023, 12:44 PM
Amran
Amran - avatar