将句子编码为猪拉丁语
在这个问题中,我们将把句子转换为猪拉丁语。我们可以将每个单词的第一个字符附加到最后,并在其后附加“ay”。
我们将看到三种将给定句子转换为猪拉丁语的方法。逻辑是我们可以将第一个字符附加到末尾,并将其从字符串中删除。之后,我们可以将“ay”附加到单词。
问题陈述 – 我们给定一个包含多个单词的字符串 alpha。我们需要将字符串编码为猪拉丁语。
注意 – 猪拉丁语是一种单词加密技术,它对单词进行一次左旋转,并在字符串末尾附加“ay”。
示例
输入
alpha = "Welcome to the tutorialspoint!";
输出
elcomeWay otay hetay utorialspoint!tay
解释 – 我们将字符串的每个单词转换为猪拉丁语。
• Welcome -> elcomeWay
• to -> otay
• the -> hetay
• tutorialspoint! -> utorialspoint!tay
输入
alpha = ‘How are you’
输出
owHay reaay ouyay
解释 – 字符串被转换为猪拉丁语。
方法 1
在这种方法中,我们将使用 substr() 方法获取字符串的特定单词。之后,我们将把第一个字符和“ay”附加到字符串的末尾。
算法
步骤 1 – 初始化“res”变量以存储结果字符串。
步骤 2 – 开始迭代字符串,并用“p”初始化“q”变量。
步骤 3 – 如果 p 大于字符串长度,则中断循环。
步骤 4 – 使用 while 循环递增“p”的值,直到我们得到空格字符并且 p 小于字符串长度。
步骤 5 – 使用 substr() 方法获取从“q + 1”索引开始且长度等于“p – q – 1”的子字符串。此外,将第一个字符和“ay”附加到末尾。
步骤 6 – 如果“res”字符串的长度为 0,则将猪拉丁语附加到“res”。否则,将空格和猪拉丁语附加到结果。
步骤 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) 用于存储猪拉丁语。
方法 2
我们在这种方法中优化了第一种方法的代码。在这种方法中,我们将在遍历字符串时获取给定字符串的每个单词,而无需使用 substr() 方法,并将每个单词转换为猪拉丁语。
算法
步骤 1 – 初始化“temp”和“res”字符串。
步骤 2 – 在遍历字符串时,如果我们得到字符,则将其附加到“temp”字符串。
步骤 3 – 如果当前字符是空格,则访问“temp”字符串的第一个字符,并将其附加到自身。此外,将“ay”附加到“temp”字符串。
步骤 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) 用于存储猪拉丁语字符串。
方法 3
在这种方法中,我们将使用空格作为分隔符来分割字符串,以获取字符串的所有单词。之后,我们将更新字符串的每个单词并将其附加到新字符串。
算法
步骤 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) 用于存储结果字符串。
我们学习了三种将句子转换为猪拉丁语的方法。第一种方法使用 substr() 方法更新单词,第二种方法使用临时字符串变量获取单词并更新它。第三种方法将字符串拆分为单词数组并更新每个单词。
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP