Encoding a Sentence into Pig Latin


In this problem, we will convert the sentence into Pig Latin. We can append the first character of each word at the last and append ‘ay’ after that.

We will see three approaches to convert the given sentence into the Pig Latine. The logic is we can append the first character at the end and remove it from the string. After that, we can append ‘ay’ to the word.

Problem statement – We have given a string alpha containing multiple words. We need to encode the string into Pig Latin.

Note – The Pig Latine is a word encryption technique, which makes one left rotation of the word, and appends ‘ay’ at the end of the string.

Sample examples

Input

alpha = "Welcome to the tutorialspoint!";

Output

elcomeWay otay hetay utorialspoint!tay

Explanation – We converted each word of string to Pig Latin.

  • • Welcome -> elcomeWay

  • • to -> otay

  • • the -> hetay

  • • tutorialspoint! -> utorialspoint!tay

Input

alpha = ‘How are you’

Output

owHay reaay ouyay

Explanation – The string is converted to Pig Latin.

Approach 1

In this approach, we will use the substr() method to get the particular word of the string. After that, we will append the first character and ‘ay’ at the end of the string.

Algorithm

Step 1 – Initialize the ‘res’ variable to store the resultant string.

Step 2 – Start iterating the string, and initialize the ‘q’ variable with ‘p’.

Step 3 – If p is greater than the string length, break the loop.

Step 4 – Increment the value of ‘p’ until we get the space character and p is less than the string length using the while loop.

Step 5 – Use the substr() method to get the substring starting from the ‘q + 1’ index and length equal to ‘p – q – 1’. Also, append the first character and ‘ay’ at the end.

Step 6 – If the length of the ‘res’ string is 0, append Pig Latine to the ‘res’. Otherwise, append space and Pig Latin to the result.

Step 7 – Return the ‘res’ string.

Example

#include <bits/stdc++.h>
using namespace std;

string encryptString(string alpha) {
    string res = "";
    // Traverse string
    for (int p = 0; p < alpha.length(); p++) {
        int q = p;
        // If p is greater than or equal to alpha length.
        if (p >= alpha.length())
            break;
        // Take a word from the string
        while (p < alpha.length() && alpha[p] != ' ')
            p++;
        // For first word
        if (res.length() == 0) {
            // Put the first character at last, and append 'ay'.
            res.append(alpha.substr(q + 1, p - q - 1) + alpha[q] + "ay");
        } else // for other words
        {
            res.append(" " + alpha.substr(q + 1, p - q - 1) + alpha[q] + "ay");
        }
    }
    return res;
}
int main() {
    string alpha = "Welcome to the tutorialspoint!";
    cout << "The Pig Latin encrypted string is - " << (encryptString(alpha));
    return 0;
}

Output

The Pig Latin encrypted string is - elcomeWay otay hetay utorialspoint!tay

Time complexity – O(N*M), where N is the string length, and M is the maximum word length to get the substring.

Space complexity – O(N) to store the Pig Latin.

Approach 2

We have optimized the code of the first approach in this approach. In this approach, we will take each word of the given string while traversing the string without using the substr() method and convert each word to Pig Latin.

Algorithm

Step 1 – Initialize the ‘temp’ and ‘res’ strings.

Step 2 – While traversing the string, append to the ‘temp’ string if we get the character.

Step 3 – If the current character is space, access the first character of the ‘temp’ string, and append to itself. Also, append ‘ay’ to the ‘temp’ string.

Step 4 – Use the erase() method to remove the first character from the ‘temp’ string.

Step 5 – Reinitialize the ‘temp’ string.

Step 6 – Handle the last word of the string.

Step 7 – Return the ‘res’ string.

Example

#include <bits/stdc++.h>
using namespace std;

string encryptString(string alpha) {
    string res = "";
    string temp = "";
    // Traverse string
    for (int p = 0; p < alpha.length(); p++) {
        // If space is found
        if (alpha[p] == ' ') {
            // Encode to Pig Latine
            temp = temp + temp[0];
            // Remove the first character
            temp.erase(temp.begin());
            // Add 'ay' at the end
            temp = temp + "ay ";
            res += temp;
            temp = "";
        } else // For other characters
        {
            temp += alpha[p];
        }
    }
    // Handling the last word
    temp = temp + temp[0];
    temp.erase(temp.begin());
    temp = temp + "ay ";
    res += temp;
    return res;
}
int main() {
    string alpha = "Welcome to the tutorialspoint";
    cout << "The Pig Latin encrypted string is - " << (encryptString(alpha));
    return 0;
}

Output

The Pig Latin encrypted string is - elcomeWay otay hetay utorialspointtay

Time complexity – O(N) to iterate the string.

Space complexity – O(N) to store the Pig Latin string.

Approach 3

In this approach, we will split the string using the space as a delimiter to get all words of the string. After that, we will update each word of the string and append it to the new string.

Algorithm

Step 1 – Initialize the ‘pig’ string.

Step 2 – Use the ‘stringstream’ to get the stream of words.

Step 3 – Start traversing the string stream, and get a single word in each iteration.

Step 4 – Use the substr() method to remove the first character from the word and append the first character at the end. Also, append ‘ay’ at last.

Step 5 – Append the updated word at the end of the ‘pig’ string.

Step 6 – Return the ‘pig’ string.

Example

#include <bits/stdc++.h>
using namespace std;

string encryptString(string alpha) {
    string pig = "";
    // Split the string by space
    stringstream words(alpha);
    string singleWord;
    // Get each words
    while (words >> singleWord) {
        singleWord = singleWord.substr(1) + singleWord[0] + "ay";
        pig += singleWord + " ";
    }
    return pig;
}
int main() {
    string alpha = "Welcome to the tutorialspoint";
    cout << "The Pig Latin encrypted string is - " << (encryptString(alpha));
    return 0;
}

Output

The Pig Latin encrypted string is - elcomeWay otay hetay utorialspointtay

Time complexity – O(N) to traverse each word of the string.

Space complexity – O(N) to store the resultant string.

We learned three approaches to converting a sentence to Pig Latin. The first approach uses the substr() method to update the word, and the second approach uses the temporary string variable to get the word and update it. The third approach splits the string into an array of words and updates each word.

Updated on: 25-Aug-2023

116 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements