Encoding a word into Pig Latin

Design a program to take a word as an input, and then encode it into a Pig Latin. A Pig Latin is an encrypted word in English, which is generated by doing following alterations:

The first vowel occurring in the input word is placed at the start of the new word along with the remaining alphabets of it. The alphabets present before the first vowel are shifted at the end of the new word followed by “ay”.
Examples:

Input: s = "paris"
Output: arispay

Input: 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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.

chevron_right



Output :

aphicgray

This article is contributed by dewangNautiyal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up

Intern at geeksForgeeks

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : vt_m, Rajput-Ji



Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.