+ 3
Piglatin [challenge question](medium)
Please suggest way to reduce the https://code.sololearn.com/cVErOZ6sUJpK/?ref=app
6 Answers
+ 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
+ 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 ");
}
}
}
+ 2
Do you want to reduce this or better way to do this?
+ 2
Jayakrishna🇮🇳 both
+ 2
Jayakrishna🇮🇳 thanks
+ 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++;
}
}
}