📜  将气味编码为 Pig Latin

📅  最后修改于: 2022-05-13 01:57:07.798000             🧑  作者: Mango

将气味编码为 Pig Latin

设计一个程序,将一个句子作为输入,然后将其编码为 Pig Latin。

例子:

方法:可以通过遍历句子,逐字修改来解决问题,如下:

  1. 找到单词第一个字母的索引。
  2. 通过附加以下步骤来创建 Pig Latin:
    • 将第一个字母附加到单词子字符串的末尾。
    • 在附加第一个字母后添加“ay”。
  3. 继续上述步骤,直到字符串中的最后一个单词。

下面是实现该方法的Java程序:

C++
#include 
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 impplement
// 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 impplement
# 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 impplement
// 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


输出:
allysay nowskay estbay

时间复杂度: O(N),其中 N 是句子的长度
辅助空间: O(1)