+ 3

Piglatin [challenge question](medium)

Please suggest way to reduce the https://code.sololearn.com/cVErOZ6sUJpK/?ref=app

18th Mar 2022, 4:44 PM
Md Saif Ali
Md Saif Ali - avatar
6 Respostas
+ 2
//by using indexOf ,& substring methods , .. import java.util.Scanner ; public class Program { public static void main(String[] args) { Scanner sc=new Scanner(System.in); String s=sc.nextLine().trim(); int l=s.length(); for(int i=0,id =0; i < l; id++, i=id) { id = s.indexOf(' ',id); if(id == -1) id = l; System.out.print(s.substring(i+1,id)+s.charAt(i)+"ay "); } } } //you are welcome... Md Saif ali
18th Mar 2022, 8:13 PM
Jayakrishna 🇮🇳
+ 3
Let Scanner do the word parsing for you. import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner input = new Scanner(System.in); String word; while (input.hasNext()) { word = input.next(); System.out.print(word.substring(1) + word.charAt(0) + "ay "); } } }
18th Mar 2022, 9:03 PM
Brian
Brian - avatar
+ 2
Do you want to reduce this or better way to do this?
18th Mar 2022, 5:04 PM
Jayakrishna 🇮🇳
18th Mar 2022, 5:53 PM
Md Saif Ali
Md Saif Ali - avatar
18th Mar 2022, 8:03 PM
Md Saif Ali
Md Saif Ali - avatar
+ 1
import java.util.Scanner ; public class Program { public static void main(String[] args) { String s,ns; Scanner sc=new Scanner(System.in); s=sc.nextLine().trim(); int l=s.length(); int counter=0; for(int i=0; i<l-1; i++) { if((s.charAt(i)==' ' && s.charAt(i+1)!=' ') || (i+2==l) ) { if( i+2==l ){ i = i+2; counter++; } ns=s.substring(i-counter,i).trim(); counter=0; ns = ( ns.length()>1 ) ? ns.substring(1)+ns.charAt(0)+"ay" : ns+"ay" ; System.out.print(ns+ " "); } counter++; } } }
18th Mar 2022, 7:35 PM
Jayakrishna 🇮🇳