把一个词译成拉丁语

设计一个程序,将一个单词作为输入,然后将其编码成拉丁语。Pig Latin是英语中的一个加密单词,通过以下修改生成: 输入单词中的第一个元音与剩余的字母一起放在新词的开头。字母表出现在第一个元音移位之前,在新词末尾紧跟着“ay”。

null

例如:

Input: s = "paris"Output: arispayInput: s = "amazon"Output: amazonay 
1) Find index of first vowel.2) Create pig latin by appending following three......a) Substring after starting with the first vowel........till end......b) Substring before first vowel......c) "ay".

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

本文由 德旺纳提亚 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞8 分享