Skip to content
Related Articles

Related Articles

Encoding a scentence into Pig Latin

Improve Article
Save Article
Like Article
  • Last Updated : 26 May, 2022

Design a program to take a sentence as an input, and then encode it into Pig Latin. 
 

A Pig Latin is an encrypted word in English, generated by placing the first letter of each word at the end, and then adding “ay” to the end.

Examples:

Input: s = “nevermind youve got them”
Output: “evermindnay ouveyay otgay hemtay”

Input: s = “sally knows best”
Output: “allysay nowskay estbay”

 

Approach: The problem can be solved by traversing the sentence and making the changes word by word, as follows:

  1. Find the index of the first letter of the word.
  2. Create a Pig Latin by appending the following these steps:
    • Append the first letter at the end of the substring of the word.
    • Add “ay” after appending the first letter.
  3. Continue the above steps till the last word in the string.

Below is the java program to implement the approach:

C++




#include <bits/stdc++.h>
using namespace std;
 
string pigLatinSentence(string s){
  string ans = "";
  for (int i = 0; i < s.length(); i++) {
    int j = i;
    if (i >= s.length())
      break;
    while (i < s.length() && s[i] != ' ')
      i++;
    if (ans.length() == 0) {
      ans.append(s.substr(j + 1,  i - j - 1) + s[j] + "ay");
    }
    else {
      ans.append(" " + s.substr(j + 1,  i - j - 1) + s[j] + "ay");
    }
  }
  return ans;
}
 
// Driver code
int main() {
 
  string s = "sally knows best";
  cout << (pigLatinSentence(s));
  return 0;
}
 
// This code is contributed by hrithikgarg03188.

Java




// Java program to implement
// the above approach
import java.io.*;
 
class GFG {
 
    public static String pigLatinSentence(String s)
    {
        String ans = "";
        for (int i = 0; i < s.length(); i++) {
            int j = i;
            if (i >= s.length())
                break;
            while (i < s.length() && s.charAt(i) != ' ')
                i++;
            if (ans.isEmpty()) {
                ans = ans.concat(
                    s.substring(j + 1, i)
                    + s.charAt(j) + "ay");
            }
            else {
                ans = ans.concat(
                    " " + s.substring(j + 1, i)
                    + s.charAt(j) + "ay");
            }
        }
        return ans;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        String s = "sally knows best";
        System.out.println(pigLatinSentence(s));
    }
}

Python3




# Python program to implement
# the above approach
def pigLatinSentence(s):
    ans = "";
    i =0;
    for i1 in range(len(s)):
        j = i;
        if (i >= len(s)):
            break;
        while (i < len(s) and s[i] != " "):
            i += 1;
        if (len(ans) == 0):
            ans = s[j + 1: i] + str(s[j:j+1]) + "ay";
        else:
            ans += " " + s[j + 1: i] + str(s[j:j+1]) + "ay";
        i += 1;
 
    return ans;
 
# Driver code
if __name__ == '__main__':
    s = "sally knows best";
    print(pigLatinSentence(s));
 
# This code is contributed by 29AjayKumar

C#




// C# program to implement
// the above approach
using System;
 
class GFG {
 
    public static string pigLatinSentence(string s)
    {
        string ans = "";
        for (int i = 0; i < s.Length; i++) {
            int j = i;
            if (i >= s.Length)
                break;
            while (i < s.Length && s[i] != ' ')
                i++;
            if (ans.Length == 0) {
                ans = ans + s.Substring(j + 1, i - j - 1)
                      + s[j] + "ay";
            }
            else {
                ans = ans + " "
                      + s.Substring(j + 1, i - j - 1) + s[j]
                      + "ay";
            }
        }
        return ans;
    }
 
    // Driver code
    public static void Main(string[] args)
    {
        string s = "sally knows best";
        Console.WriteLine(pigLatinSentence(s));
    }
}
 
// This code is contributed by ukasp.

Javascript




<script>
    // JavaScript program to implement
    // the above approach
 
    const pigLatinSentence = (s) => {
        let ans = "";
        for (let i = 0; i < s.length; i++) {
            let j = i;
            if (i >= s.length)
                break;
            while (i < s.length && s.charAt(i) != ' ')
                i++;
            if (ans.length === 0) {
                ans = ans + s.substring(j + 1, i) + s.charAt(j) + "ay";
            }
            else {
                ans = ans + " " + s.substring(j + 1, i) + s.charAt(j) + "ay";
            }
        }
        return ans;
    }
 
    // Driver code
 
    let s = "sally knows best";
    document.write(pigLatinSentence(s));
 
    // This code is contributed by rakeshsahni
 
</script>
Output: 
allysay nowskay estbay

 

Time Complexity: O(N), where N is the length of sentence 
Auxiliary Space: O(1) 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!