0
Please explain the exception coming in my program.
In this program, I have tried the pig latin code coach which takes a string as input and removes the first letter of each word, add "ay" to the end of that word then, give it as output. My approach: I am taking a string as input then, I trimmed it using.trim() method and I used .split() method and stored every word as an element of an array then by using .substring() I took the required the part of the word and added "ay" to it. https://code.sololearn.com/caV9T5cmyF20/?ref=app
2 Respostas
+ 2
you don't remove the first letter, you move it to the end and then add "ay". eg "nice" becomes "icenay", not "iceay"
additionally, vowels are handled differently: don't remove the first letter, just add "yay" to the end. eg "opera" becomes "operayay"
also double check the format asked for in the instructions because sometimes it's done with a hyphen, eg ice-nay and opera-yay
finally, this is probably beyond the scope of what they're asking for, but to do it *properly* you'd want to look for consonant clusters instead of just single letters, eg child, thief, shoe, price, and string would become ild-chay, ief-thay, oe-shay, ice-pray, and ing-stray.
0
import java.util.Scanner;
public class Program
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
// String str1 = str.trim();
String[] str2 = str.split(" ");
// System.out.println(str2[0].length());
for(int i=0; i<str2.length; i++) { //debug
System.out.print(str2[i].substring(1,str2[i].length()) + str2[i].substring(0,1) + "ay" + " "); //debug
}
}
}