C++ 将句子转换成 Pig Latin

C++ 将句子转换成 Pig Latin

在这个问题中,我们将把句子转换成 Pig Latin。我们可以将每个单词的第一个字符附加在后面,然后再附加‘ay’。

我们将介绍三种将给定句子转换成 Pig Latin 的方法。逻辑是我们可以将第一个字符附加到后面,并从字符串中删除它。然后,我们可以在单词后面附加‘ay’。

问题描述 - 我们给定一个包含多个单词的字符串 alpha。我们需要将该字符串编码为 Pig Latin。

注意 - Pig Latin 是一种单词加密技术,它将单词左移一位,并在字符串末尾附加‘ay’。

示例示例

输入

alpha = "Welcome to the tutorialspoint!";

输出

elcomeWay otay hetay utorialspoint!tay

解释 - 我们将字符串的每个单词转换为Pig Latin。

  • • Welcome -> elcomeWay

  • • to -> otay

  • • the -> hetay

  • • tutorialspoint! -> utorialspoint!tay

输入

alpha = ‘How are you’

输出

owHay reaay ouyay

解释 - 将字符串转换为Pig Latin。

方法一

在这种方法中,我们将使用substr()方法获取字符串的特定单词。然后,在字符串的末尾添加第一个字符和“ay”。

算法

步骤 1 - 初始化“res”变量以存储结果字符串。

步骤 2 - 开始遍历字符串,并用“p”初始化“q”变量。

步骤 3 - 如果“p”大于字符串长度,则退出循环。

步骤 4 - 使用while循环增加“p”的值,直到获取到空格字符且“p”小于字符串长度。

步骤 5 - 使用substr()方法从“q + 1”索引开始获取子字符串,长度为“p – q – 1”。同时,在末尾添加第一个字符和“ay”。

步骤 6 - 如果“res”字符串的长度为0,则将Pig Latin附加到“res”字符串。否则,将空格和Pig Latin附加到结果字符串上。

步骤 7 - 返回“res”字符串。

示例

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

输出结果

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

时间复杂度 – O(N*M),其中N是字符串的长度,M是获取子串的最大单词长度。

空间复杂度 – O(N),用于存储Pig Latin。

方法2

我们在这种方法中优化了第一种方法的代码。在这种方法中,我们将在遍历字符串时不使用substr()方法,而是从给定字符串中取出每个单词,并将每个单词转换为Pig Latin。

算法

步骤1 - 初始化’temp’和’res’字符串。

步骤2 - 在遍历字符串时,如果获取到字符,则将其追加到’temp’字符串中。

步骤3 - 如果当前字符是空格,则访问’temp’字符串的第一个字符,并将其追加到自身。同时,在’temp’字符串末尾追加’ay’。

步骤4 - 使用erase()方法从’temp’字符串中删除第一个字符。

步骤5 - 重新初始化’temp’字符串。

步骤6 - 处理字符串的最后一个单词。

步骤7 - 返回’res’字符串。

示例

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

输出

The Pig Latin encrypted string is - elcomeWay otay hetay utorialspointtay

时间复杂度 – O(N),遍历字符串。

空间复杂度 – O(N),存储Pig Latin字符串。

第三种方法

在这种方法中,我们将使用空格作为分隔符来拆分字符串,以获取字符串的所有单词。然后,我们将更新字符串的每个单词并将其附加到新字符串中。

算法

第1步 - 初始化’pig’字符串。

第2步 - 使用’stringstream’来获取单词流。

第3步 - 开始遍历字符串流,在每次迭代中获取一个单词。

第4步 - 使用substr()方法删除单词的第一个字符,并将第一个字符附加到末尾。同时,在最后附加’ay’。

第5步 - 将更新后的单词附加到’pig’字符串的末尾。

第6步 - 返回’pig’字符串。

示例

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

输出

The Pig Latin encrypted string is - elcomeWay otay hetay utorialspointtay

时间复杂度 – O(N) 遍历字符串的每个单词。

空间复杂度 – O(N) 存储结果字符串。

我们学习了将句子转换为 Pig Latin 的三种方法。第一种方法使用 substr() 方法来更新单词,第二种方法使用临时字符串变量来获取单词并更新它。第三种方法将字符串拆分为单词数组并更新每个单词。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程