In: Computer Science
Pig Latin is a language constructed by transforming English words. The following rules are used to translate English into Pig Latin:
*If the word begins with a consonant, then all consonants at the beginning of the word, up to the first vowel are removed then added to the end of the word, followed by “ay”. For example, “computer” becomes “omputercay” and “think” becomes “inkthay”.
*If the word begins with a vowel, then “way” is added to the end of the word. For example, “algorithm” becomes “algorithmway” and “office” becomes “officeway”.
*If the word ends in a punctuation mark, then the punctuation mark should remain at the end of the word after the transformation has been performed. For example, “science!” should become “iencescay!”. You can assume that the punctuation mark is only a single character. The program reads a line of text from the user and translates it into Pig Latin and displays the result.
This is in python.
input code:
output:
code:
import string
'''take user input'''
text=input("Enter a Text: ")
'''break the string and store into list'''
text=list(text.split(" "))
'''declare the list of vowels'''
vowels=['A','E','I','O','U','a','e','i','o','u']
output=[]
'''convert all the words into pig Latin'''
for i in text:
'''check ends with punctuation or not'''
if i[len(i)-1] not in string.punctuation:
'''if not end than check start with vowels or not'''
if i[0] in vowels:
'''true than add way'''
i=i+"way"
else:
'''else for loop until we get vowels'''
for ch in range(len(i)-1):
'''if we find vowels'''
if i[ch] in vowels:
'''than do this and break the loop'''
i=i[ch:]+i[0:ch]+"ay"
break
'''add value into list'''
output.append(i)
else:
'''if end than check start with vowels or not'''
if i[0] in vowels:
'''true than add way and than add punctuation'''
i=i[:len(i)-1]+"way"+i[len(i)-1]
else:
'''else for loop until we get vowels'''
for ch in range(len(i)-1):
'''if we find vowels'''
if i[ch] in vowels:
'''than do this and break the loop'''
i=i[ch:len(i)-1]+i[0:ch]+"ay"+i[len(i)-1]
break
'''add value into list'''
output.append(i)
'''make list to string'''
output=" ".join(output)
'''print output'''
print("Pig Latin text: ",output)