📜  将字符串的每个字符替换为 ASCII 值比它大 K 倍的字符

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

将字符串的每个字符替换为 ASCII 值比它大 K 倍的字符

给定字符串str仅由小写字母和整数k组成,任务是将给定字符串的每个字符替换为 ASCII 值比它大 k 倍的字符。如果 ASCII 值超过“z”,则以循环方式从“a”开始检查。

例子:

方法:迭代字符串中的每个字符并为每个字符执行以下步骤:

  • 将 k 添加到字符str[i] 的 ASCII 值。
  • 如果超过 122,则执行 k 与 26 的模运算以减少步数,因为 26 是旋转中可以执行的最大移位数。
  • 要找到字符,请将 k 添加到 96。因此,具有 ASCII 值 k+96 的字符将是一个新字符。

对给定字符串的每个字符重复上述步骤。

下面是上述方法的实现:

C++
// CPP program to move every character
// K times ahead in a given string
#include 
using namespace std;
 
// Function to move string character
void encode(string s,int k){
 
    // changed string
    string newS;
 
    // iterate for every characters
    for(int i=0; i 122){
            k -= (122-val);
            k = k % 26;
            newS += char(96 + k);
        }
        else
            newS += char(val + k);
 
        k = dup;
    }
 
    // print the new string
    cout<


Java
// Java program to move every character
// K times ahead in a given string
 
class GFG {
 
// Function to move string character
    static void encode(String s, int k) {
 
        // changed string
        String newS = "";
 
        // iterate for every characters
        for (int i = 0; i < s.length(); ++i) {
            // ASCII value
            int val = s.charAt(i);
            // store the duplicate
            int dup = k;
 
            // if k-th ahead character exceed 'z'
            if (val + k > 122) {
                k -= (122 - val);
                k = k % 26;
                 
                newS += (char)(96 + k);
            } else {
                newS += (char)(val + k);
            }
 
            k = dup;
        }
 
        // print the new string
        System.out.println(newS);
    }
 
// Driver Code
    public static void main(String[] args) {
        String str = "abc";
        int k = 28;
 
        // function call
        encode(str, k);
    }
}
 
// This code is contributed by Rajput-JI


Python3
# Python program to move every character
# K times ahead in a given string
 
# Function to move string character
def encode(s, k):
     
    # changed string
    newS = ""
     
    # iterate for every characters
    for i in range(len(s)):
         
        # ASCII value
        val = ord(s[i])
         
        # store the duplicate
        dup = k
         
        # if k-th ahead character exceed 'z'
        if val + k>122:
            k -= (122-val)
            k = k % 26
            newS += chr(96 + k)
             
        else:
            newS += chr(val + k)
         
        k = dup
     
    # print the new string
    print (newS)
             
# driver code    
str = "abc"
k = 28
 
encode(str, k)


C#
// C# program to move every character
// K times ahead in a given string
using System;
public class GFG {
 
// Function to move string character
    static void encode(String s, int k) {
 
        // changed string
        String newS = "";
 
        // iterate for every characters
        for (int i = 0; i < s.Length; ++i) {
            // ASCII value
            int val = s[i];
            // store the duplicate
            int dup = k;
 
            // if k-th ahead character exceed 'z'
            if (val + k > 122) {
                k -= (122 - val);
                k = k % 26;
                 
                newS += (char)(96 + k);
            } else {
                newS += (char)(96 + k);
            }
 
            k = dup;
        }
 
        // print the new string
        Console.Write(newS);
    }
 
// Driver Code
    public static void Main() {
        String str = "abc";
        int k = 28;
 
        // function call
        encode(str, k);
    }
}
 
// This code is contributed by Rajput-JI


PHP
 122)
        {
            $k -= (122 - $val);
            $k = $k % 26;
            $newS = $newS.chr(96 + $k);
        }
        else
            $newS = $newS.chr($val + $k);
 
        $k = $dup;
    }
 
    // print the new string
    echo $newS;
}
 
// Driver code
 
$str = "abc";
$k = 28;
 
// function call
encode($str, $k);
 
// This code is contributed by ita_c
?>


Javascript


输出:

cde

时间复杂度: O(N),其中 N 是字符串的长度。