Encoding a sentence into Pig Latin
Last Updated :
24 Aug, 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:
- Find the index of the first letter of the word.
- 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.
- 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;
}
int main() {
string s = "sally knows best" ;
cout << (pigLatinSentence(s));
return 0;
}
|
Java
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;
}
public static void main(String[] args)
{
String s = "sally knows best" ;
System.out.println(pigLatinSentence(s));
}
}
|
Python3
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;
if __name__ = = '__main__' :
s = "sally knows best" ;
print (pigLatinSentence(s));
|
C#
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;
}
public static void Main( string [] args)
{
string s = "sally knows best" ;
Console.WriteLine(pigLatinSentence(s));
}
}
|
Javascript
<script>
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;
}
let s = "sally knows best" ;
document.write(pigLatinSentence(s));
</script>
|
Output:
allysay nowskay estbay
Time Complexity: O(N), where N is the length of sentence
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...