📌  相关文章
📜  通过将每个字符的字母值向前移动来编码给定的字符串

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

通过将每个字符的字母值向前移动来编码给定的字符串

给定由小写英文字母组成的大小为N的字符串str ,任务是将给定字符串编码如下:

  • 将该字符串的每个字符更改为另一个字符
  • 改变的字符和当前字符之间的距离与当前字符和'a'之间的距离相同。
  • 此外,假设字符的数组形成一个循环,即在“z”之后,循环又从“a”开始。

例子:

方法:这个问题可以通过以下步骤来解决:

  • 运行从i=0i的循环并遍历字符串的每个字符。对于每个字符str[i]
    • str[i]和 'a' 之间的距离,即dist=str[i]-'a'
    • 现在,如果(dist+(str[i]-'a')) > 26 ,这意味着超过了 'z',所以
  • 否则,将str[i]更改为str[i]+dist
  • 打印字符串作为此问题的答案。

下面是上述方法的实现:

C++
// C++ code for the above approach
#include 
using namespace std;
 
// Function to change every character
// of the string to another character
void changeString(string str)
{
    for (auto& x : str) {
        int dist = x - 'a';
 
        // If 'z' is exceeded
        if (dist + (x - 'a') > 26) {
            dist = (dist + (x - 'a')) % 26;
            x = 'a' + dist;
        }
 
        // If 'z' is not exceeded
        else {
            x = x + dist;
        }
    }
    cout << str << endl;
}
 
// Driver Code
int main()
{
    string str = "cycleofalphabet";
    changeString(str);
    return 0;
}


Java
// Jsvs code for the above approach
import java.util.*;
 
class GFG {
 
  // Function to change every character
  // of the string to another character
  static void changeString(String str)
  {
    char[] ch = str.toCharArray();
    for (int i = 0; i < str.length(); i++) {
      int dist = ch[i] - 'a';
 
      // If 'z' is exceeded
      if (dist + (ch[i] - 'a') > 26) {
        dist = (dist + (ch[i] - 'a')) % 26;
        ch[i] = (char)('a' + dist);
      }
 
      // If 'z' is not exceeded
      else {
        ch[i] = (char)(ch[i] + dist);
      }
    }
 
    String s = new String(ch);
    System.out.println(s);
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    String str = "cycleofalphabet";
    changeString(str);
  }
}
 
// This code is contributed by ukasp.


Python3
# Python code for the above approach
 
# Function to change every character
# of the string to another character
def changeString(str):
    str = list(str)
    for x in range(len(str)):
        dist = ord(str[x]) - ord('a')
 
        # If 'z' is exceeded
        if (dist + (ord(str[x]) - ord('a')) > 26):
            dist = (dist + (ord(str[x]) - ord('a'))) % 26;
            str[x] = chr(ord('a') + dist);
         
 
        # If 'z' is not exceeded
        else:
            str[x] = chr(ord(str[x]) + dist);
         
    str = "".join(str)
    print(str)
 
# Driver Code
 
str = "cycleofalphabet";
changeString(str);
 
# This code is contributed by Saurabh Jaiswal


C#
// C# code for the above approach
using System;
using System.Collections;
 
class GFG
{
 
  // Function to change every character
  // of the string to another character
  static void changeString(string str)
  {
    char[] ch = str.ToCharArray(); 
    for(int i = 0; i < str.Length; i++) {
      int dist = ch[i] - 'a';
 
      // If 'z' is exceeded
      if (dist + (ch[i] - 'a') > 26) {
        dist = (dist + (ch[i] - 'a')) % 26;
        ch[i] = (char)('a' + dist);
      }
 
      // If 'z' is not exceeded
      else {
        ch[i] = (char)(ch[i] + dist);
      }
    }
 
    string s = new string(ch);
    Console.WriteLine(s);
  }
 
  // Driver Code
  public static void Main()
  {
    string str = "cycleofalphabet";
    changeString(str);
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript



输出
ewewickaweoacim

时间复杂度: O(N)
辅助空间: O(1)