![piglatin translator piglatin translator](https://static.javatpoint.com/core/images/pig-latin-program-in-java.png)
![piglatin translator piglatin translator](https://joekarlsson.github.io/Pig-Latin/img/my_logo_450.png)
The str.substring(0, vowel) will return nothing because substring(0,0) is nothing. If the string doesn’t have a vowel, str.substring(vowel) will just return the entire string. When we concatenate both strings together and slap on an “ay” at the end, we have our Pig Latin string. The str.substring(0, vowel) represents the consonant or the consonant cluster. The str.substring(vowel) represents the section of the string without the consonant beginning.
![piglatin translator piglatin translator](https://i.ytimg.com/vi/v4sXzQYCL48/maxresdefault.jpg)
Now that we have the index of the first occurring vowel, we use that number as a start value argument for our substring() method. Once again if firstMatch is 0, the vowel variable doesn’t matter. Since match() gives us an array, we only care about the first item in the array. The vowel variable will give us the index of the first vowel found in the string using indexOf(). If there are no vowels, then we assign firstMatch the value of 0. from time import sleep def main (): topiglatin input ('Do you want to translate.
#Piglatin translator code#
I would just like it if someone reviewed my code and see if anything can be done to make it more Pythonic or efficient. Our pattern looks at any character that is a vowel. I am a relatively new Python programmer and made a simple Pig Latin to English translator and vice versa. What match() does is it returns an array of all the character indexes that match our regular expression pattern. let firstMatch = str.match(//g) || 0 let vowel = str.indexOf(firstMatch) newStr = str.substring(vowel) + str.substring(0, vowel) + "ay" return newStr įor our firstMatch variable, we use the regular expression method match(). If the first character isn’t a vowel then we move on to our else block. If it does, we add “way” to the end of the string and return newStr. We use the indexOf() method to check if the first character exists in our vowels array. Our if-statement checks if the first letter of the string is a vowel. Next, we write a conditional that looks at the first character of our string input. The newStr variable is going to hold our Pig Latin string. We will use this to check if the first character of str is a vowel. The vowels variable is an array of vowels. First, we will create two variables: let vowels = let newStr = ""