+ 2

Can anybody tell me what's wrong in this code, it is creating problems with input with spaces. E. Vegetable pizza.

import java.util.*; public class Program { public static void main(String[] args) { Scanner sc=new Scanner(System.in); String str=sc.nextLine(); String nstr=str.toLowerCase(); String a="abcdefghijklmnopqrstuvwxyz"; for(int i=0;i<=10;i++) { int c=0; for(int j=0;j<26;j++){ c++; if(nstr.charAt(i)==' '){ System.out.print(" "); c=c-1; } else if(nstr.charAt(i)==a.charAt(j)){ break; } }//closing of j System.out.print(a.charAt(26-c)); } } }

18th Sep 2020, 12:30 PM
Lucifer 👩‍💻
Lucifer 👩‍💻 - avatar
6 Réponses
+ 2
https://code.sololearn.com/cNk92Od5tmZC/?ref=app, Thanks brother for your kind help.
20th Sep 2020, 4:55 AM
Lucifer 👩‍💻
Lucifer 👩‍💻 - avatar
0
Check by taking input vegetable pizza
18th Sep 2020, 1:21 PM
Lucifer 👩‍💻
Lucifer 👩‍💻 - avatar
0
I replaced (i<=10)with ( l-1),where l=length of inputting string. Then also amen problem occurs. Here's the new one. import java.util.*; public class Program { public static void main(String[] args) { Scanner sc=new Scanner(System.in); String str=sc.nextLine(); String nstr=str.toLowerCase(); int l=nstr.length(); String a="abcdefghijklmnopqrstuvwxyz"; for(int i=0;i<=(l-1);i++) { int c=0; for(int j=0;j<26;j++){ c++; if(nstr.charAt(i)==' '){ System.out.print(" "); c=c-1; } else if(nstr.charAt(i)==a.charAt(j)){ break; } }//closing of j System.out.print(a.charAt(26-c)); } } }
19th Sep 2020, 9:53 AM
Lucifer 👩‍💻
Lucifer 👩‍💻 - avatar
0
Rithea Sreng,but i removed it then why the same error is occurring.
19th Sep 2020, 10:00 AM
Lucifer 👩‍💻
Lucifer 👩‍💻 - avatar
0
The outcome of vegetable pizza should be 'evtvgzyov kraaz'.
19th Sep 2020, 10:01 AM
Lucifer 👩‍💻
Lucifer 👩‍💻 - avatar
0
I didn't understand what you are trying to say.
19th Sep 2020, 11:29 AM
Lucifer 👩‍💻
Lucifer 👩‍💻 - avatar