📌  相关文章
📜  修改字符串,使每个字符都替换为键盘中的下一个字符

📅  最后修改于: 2021-10-28 01:43:14             🧑  作者: Mango

给定一个由小写英文字母组成的字符串str 。任务是使用键盘中的下一个字母(以循环方式)键更改字符串的每个字符。例如, ‘a’被替换为‘s’‘b’被替换为‘n’ ,….., ‘l’被替换为‘z’ ,….., ‘m’被替换为‘q’ .
例子:

方法:对于英文字母表中的每个小写字符,在 unordered_map 中将其旁边的字符插入到键盘中。现在,通过遍历字符的给定字符串的字符,并更新与先前创建的地图的每一个字符。
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
const string CHARS = "qwertyuiopasdfghjklzxcvbnm";
const int MAX = 26;
 
// Function to return the modified string
string getString(string str, int n)
{
 
    // Map to store the next character
    // on the keyboard for every
    // possible lowercase character
    unordered_map uMap;
    for (int i = 0; i < MAX; i++) {
        uMap[CHARS[i]] = CHARS[(i + 1) % MAX];
    }
 
    // Update the string
    for (int i = 0; i < n; i++) {
        str[i] = uMap[str[i]];
    }
 
    return str;
}
 
// Driver code
int main()
{
    string str = "geeks";
    int n = str.length();
 
    cout << getString(str, n);
 
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
 
class GFG
{
 
static String CHARS = "qwertyuiopasdfghjklzxcvbnm";
static int MAX = 26;
 
// Function to return the modified String
static String getString(char[] str, int n)
{
 
    // Map to store the next character
    // on the keyboard for every
    // possible lowercase character
    Map uMap = new HashMap<>();
    for (int i = 0; i < MAX; i++)
    {
        uMap. put(CHARS.charAt(i),
                  CHARS.charAt((i + 1) % MAX));
    }
 
    // Update the String
    for (int i = 0; i < n; i++)
    {
        str[i] = uMap.get(str[i]);
    }
    return String.valueOf(str);
}
 
// Driver code
public static void main(String []args)
{
    String str = "geeks";
    int n = str.length();
 
    System.out.println(getString(str.toCharArray(), n));
}
}
 
// This code is contributed by Rajput-Ji


Python3
# Python3 implementation of the approach
 
CHARS = "qwertyuiopasdfghjklzxcvbnm";
MAX = 26;
 
# Function to return the modified string
def getString(string, n) :
 
    string = list(string);
     
    # Map to store the next character
    # on the keyboard for every
    # possible lowercase character
    uMap = {};
     
    for i in range(MAX) :
        uMap[CHARS[i]] = CHARS[(i + 1) % MAX];
         
    # Update the string
    for i in range(n) :
        string[i] = uMap[string[i]];
         
    return "".join(string);
 
# Driver code
if __name__ == "__main__" :
 
    string = "geeks";
    n = len(string);
 
    print(getString(string, n));
 
# This code is contributed by AnkitRai01


C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
 
class GFG
{
 
static String CHARS = "qwertyuiopasdfghjklzxcvbnm";
static int MAX = 26;
 
// Function to return the modified String
static String getString(char[] str, int n)
{
 
    // Map to store the next character
    // on the keyboard for every
    // possible lowercase character
    Dictionary uMap = new Dictionary();
    for (int i = 0; i < MAX; i++)
    {
        if(!uMap.ContainsKey(CHARS[i]))
            uMap.Add(CHARS[i], CHARS[(i + 1) % MAX]);
        else
            uMap[CHARS[i]] = CHARS[(i + 1) % MAX];
    }
 
    // Update the String
    for (int i = 0; i < n; i++)
    {
        str[i] = uMap[str[i]];
    }
    return String.Join("", str);
}
 
// Driver code
public static void Main(String []args)
{
    String str = "geeks";
    int n = str.Length;
 
    Console.WriteLine(getString(str.ToCharArray(), n));
}
}
 
// This code is contributed by PrinciRaj1992


Javascript


输出:
hrrld

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程