Respuesta :
Answer:
// here is code in java.
import java.util.*;
// class defintion
class Main
{ //main method of the class
public static void main(String[] args)
{ //scanner object to read input
Scanner sccr = new Scanner( System.in );
System.out.print("Enter your sentence: ");
/* read the sentence and split into the words and store it in the array */
String[] inp_words = sccr.nextLine().split(" ");
System.out.println("words of sentence in pig latin:");
// convert each word into the pig latin
for (String w : inp_words)
{
System.out.print(w.substring(1)+w.substring(0,1)+"ay ");
}
System.out.println();
}
}
Explanation:
Create a scanner class object to read input from user.Read the input sentence From user then split it into word and save then into a string array called "inp_words". For each word of input sentence, extract first character and append it in the end of word and then append "ay" to that string.This will change the input sentence into "Pig Latin".
Output:
Enter your sentence: hello world welcome to java
words of sentence in pig latin:
ellohay orldway elcomeway otay avajay
Answer:
sen = input('Enter sentence: ')
sen = sen.split()
new =[]
for word in sen:
new.append(word[1:]+word[0]+'ay')
new = ' '.join(new)
print(new)
Explanation:
The programming language used is python 3.
The program prompts the user to enter a sentence, it then takes the sentence and converts it to a list, using the split method, the purpose of this is to perform special operations on the text that can only be done if it is a list.
A new list is also created to hold the sentence that has been converted to Pig Latin.
for word in sen:
new.append(word[1:]+word[0]+'ay')
The block of code above utilizes a FOR loop, LIST SLICE and the append method to convert each word in the sentence to Pig Latin.
word[1:] is appending the letters of the word to a new list but starting from the second index ( i.e. The second letter) to the last index in the word.
+word[0] adds the the first index, i.e. The first letter.
+'ay' is also appended to the end of each word.
After each word has been appended to the new list, the new list is converted back to a string and the result is printed to the screen.
I have attached a picture for you to see how the code works
