Design a program to take a word as an input, and then encode it into Pig Latin. A Pig Latin is an encrypted word in English, which is generated by doing the following alterations:
The first vowel occurring in the input word is placed at the start of the new word along with the remaining alphabet of it. The alphabet is present before the first vowel is shifted, at the end of the new word it is followed by “ay”.
Examples:
Input: s = "paris"
Output: arispay
Input: s = "amazon"
Output: amazonay
- Find index of first vowel.
- Create pig latin by appending following three.
- Substring after starting with the first vowel ……..till end.
- Substring before first vowel.
- “ay”.
Implementation:
CPP
#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)
{
int len = s.length();
int index = -1;
for ( int i = 0; i < len; i++) {
if (isVowel(s[i])) {
index = i;
break ;
}
}
if (index == -1)
return "-1" ;
return s.substr(index) + s.substr(0, index) + "ay" ;
}
int main()
{
string str = pigLatin( "graphic" );
if (str == "-1" )
cout << "No vowels found. Pig Latin not possible" ;
else
cout << str;
}
|
Java
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) {
int len = s.length();
int index = - 1 ;
for ( int i = 0 ; i < len; i++)
{
if (isVowel(s.charAt(i))) {
index = i;
break ;
}
}
if (index == - 1 )
return "-1" ;
return s.substring(index) +
s.substring( 0 , index) + "ay" ;
}
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);
}
}
|
Python3
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):
length = len (s);
index = - 1 ;
for i in range (length):
if (isVowel(s[i])):
index = i;
break ;
if (index = = - 1 ):
return "-1" ;
return s[index:] + s[ 0 :index] + "ay" ;
str = pigLatin( "graphic" );
if ( str = = "-1" ):
print ( "No vowels found. Pig Latin not possible" );
else :
print ( str );
|
C#
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)
{
int len = s.Length;
int index = -1;
for ( int i = 0; i < len; i++)
{
if (isVowel(s[i]))
{
index = i;
break ;
}
}
if (index == -1)
return "-1" ;
return s.Substring(index) +
s.Substring(0, index)
+ "ay" ;
}
public static void Main()
{
string str = pigLatin( "graphic" );
if (str == "-1" )
Console.WriteLine( "No vowels"
+ "found. Pig Latin"
+ " not possible" );
else
Console.WriteLine(str);
}
}
|
Javascript
<script>
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)
{
let len = s.length;
let index = -1;
for (let i = 0; i < len; i++)
{
if (isVowel(s[i]))
{
index = i;
break ;
}
}
if (index == -1)
return "-1" ;
return s.substring(index) +
s.substring(0, index)
+ "ay" ;
}
str = pigLatin( "graphic" );
if (str == "-1" )
document.write( "No vowels"
+ "found. Pig Latin"
+ " not possible" );
else
document.write(str);
</script>
|
This article is contributed by dewangNautiyal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Login to comment...