Diseñe un programa para tomar una palabra como entrada y luego codifíquela en Pig Latin. Un Pig Latin es una palabra encriptada en inglés, que se genera al hacer las siguientes alteraciones:
La primera vocal que aparece en la palabra de entrada se coloca al comienzo de la nueva palabra junto con el resto del alfabeto. El alfabeto está presente antes de que se cambie la primera vocal, al final de la nueva palabra le sigue «ay».
Ejemplos:
Input: s = "paris" Output: arispay Input: s = "amazon" Output: amazonay
- Encuentra el índice de la primera vocal.
- Cree cerdo latino agregando los siguientes tres.
- Substring después de comenzar con la primera vocal ……..hasta el final.
- Substring antes de la primera vocal.
- «sí».
Implementación:
CPP
// C++ program to encode a word to a Pig Latin. #include <bits/stdc++.h> using namespace std; bool isVowel(char c) { return (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U' || c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'); } string pigLatin(string s) { // the index of the first vowel is stored. int len = s.length(); int index = -1; for (int i = 0; i < len; i++) { if (isVowel(s[i])) { index = i; break; } } // Pig Latin is possible only if vowels // is present if (index == -1) return "-1"; // Take all characters after index (including // index). Append all characters which are before // index. Finally append "ay" return s.substr(index) + s.substr(0, index) + "ay"; } // Driver code int main() { string str = pigLatin("graphic"); if (str == "-1") cout << "No vowels found. Pig Latin not possible"; else cout << str; }
Java
// Java program to encode a word to a Pig Latin. class GFG { static boolean isVowel(char c) { return (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U' || c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'); } static String pigLatin(String s) { // the index of the first vowel is stored. int len = s.length(); int index = -1; for (int i = 0; i < len; i++) { if (isVowel(s.charAt(i))) { index = i; break; } } // Pig Latin is possible only if vowels // is present if (index == -1) return "-1"; // Take all characters after index (including // index). Append all characters which are before // index. Finally append "ay" return s.substring(index) + s.substring(0, index) + "ay"; } // Driver code public static void main(String[] args) { String str = pigLatin("graphic"); if (str == "-1") System.out.print("No vowels found." + "Pig Latin not possible"); else System.out.print(str); } } // This code is contributed by Anant Agarwal.
Python3
# Python program to encode a word to a Pig Latin. def isVowel(c): return (c == 'A' or c == 'E' or c == 'I' or c == 'O' or c == 'U' or c == 'a' or c == 'e' or c == 'i' or c == 'o' or c == 'u'); def pigLatin(s): # the index of the first vowel is stored. length = len(s); index = -1; for i in range(length): if (isVowel(s[i])): index = i; break; # Pig Latin is possible only if vowels # is present if (index == -1): return "-1"; # Take all characters after index (including # index). Append all characters which are before # index. Finally append "ay" return s[index:] + s[0:index] + "ay"; str = pigLatin("graphic"); if (str == "-1"): print("No vowels found. Pig Latin not possible"); else: print(str); # This code contributed by Rajput-Ji
C#
// C# program to encode a word to a // Pig Latin. using System; class GFG { static bool isVowel(char c) { return (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U' || c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'); } static string pigLatin(string s) { // the index of the first // vowel is stored. int len = s.Length; int index = -1; for (int i = 0; i < len; i++) { if (isVowel(s[i])) { index = i; break; } } // Pig Latin is possible only // if vowels is present if (index == -1) return "-1"; // Take all characters after // index (including index). // Append all characters which // are before index. Finally // append "ay" return s.Substring(index) + s.Substring(0, index) + "ay"; } // Driver code public static void Main() { string str = pigLatin("graphic"); if (str == "-1") Console.WriteLine("No vowels" + "found. Pig Latin" + " not possible"); else Console.WriteLine(str); } } // This code is contributed by vt_m.
Javascript
<script> // js program to encode a word to a // Pig Latin. function isVowel(c) { return (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U' || c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'); } function pigLatin(s) { // the index of the first // vowel is stored. let len = s.length; let index = -1; for (let i = 0; i < len; i++) { if (isVowel(s[i])) { index = i; break; } } // Pig Latin is possible only // if vowels is present if (index == -1) return "-1"; // Take all characters after // index (including index). // Append all characters which // are before index. Finally // append "ay" return s.substring(index) + s.substring(0, index) + "ay"; } // Driver code str = pigLatin("graphic"); if (str == "-1") document.write("No vowels" + "found. Pig Latin" + " not possible"); else document.write(str); </script>
aphicgray
Este artículo es una contribución de dewangNautiyal . Si te gusta GeeksforGeeks y te gustaría contribuir, también puedes escribir un artículo usando write.geeksforgeeks.org o enviar tu artículo por correo a review-team@geeksforgeeks.org. Vea su artículo que aparece en la página principal de GeeksforGeeks y ayude a otros Geeks.
Publicación traducida automáticamente
Artículo escrito por dewangNautiyal y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA