📌  相关文章
📜  通过从另一给定的字符串加入相同索引的字符整数值修改字符串的字符

📅  最后修改于: 2021-09-05 11:34:57             🧑  作者: Mango

给定两个字符串S和相同的长度的N,分别包括字母和数字字符的任务是生成通过与字符串的相同索引的字符的ASCII值增加的字符串n每个字符的整数值而获得的新的字符串S上。最后,打印结果字符串。
注意:如果总和超过 122,则从总和中减去 26 并打印结果字符。

例子:

处理方法:按照以下步骤解决问题:

  • 遍历字符串S:
    • 将字符串N的当前字符转换为其等效的整数值。
    • 将获得的整数值与字符串S 中当前字符的等效 ASCII 值相加。
    • 如果该值超过122 ,即最后一个字母‘z’的 ASCII 值,则将该值减去26。
    • 通过用 ASCII 值等于获得的值的字符替换当前字符更新字符串S。
  • 完成上述步骤后打印结果字符串。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to modify a given string
// by adding ASCII value of characters
// from a string S to integer values of
// same indexed characters in string N
void addASCII(string S, string N)
{
    // Traverse the string
    for (int i = 0; i < S.size(); i++) {
 
        // Stores integer value of
        // character in string N
        int a = int(N[i]) - '0';
 
        // Stores ASCII value of
        // character in string S
        int b = int(S[i]) + a;
 
        // If b exceeds 122
        if (b > 122)
            b -= 26;
 
        // Replace the character
        S[i] = char(b);
    }
 
    // Print resultant string
    cout << S;
}
 
// Driver Code
int main()
{
    // Given strings
    string S = "sun", N = "966";
 
    // Function call to modify
    // string S by given operations
    addASCII(S, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG
{
 
// Function to modify a given String
// by adding ASCII value of characters
// from a String S to integer values of
// same indexed characters in String N
static void addASCII(char []S, char []N)
{
   
    // Traverse the String
    for (int i = 0; i < S.length; i++)
    {
 
        // Stores integer value of
        // character in String N
        int a = (int)(N[i]) - '0';
 
        // Stores ASCII value of
        // character in String S
        int b = (int)(S[i]) + a;
 
        // If b exceeds 122
        if (b > 122)
            b -= 26;
 
        // Replace the character
        S[i] = (char)(b);
    }
 
    // Print resultant String
    System.out.print(S);
}
 
// Driver Code
public static void main(String[] args)
{
   
    // Given Strings
    String S = "sun", N = "966";
 
    // Function call to modify
    // String S by given operations
    addASCII(S.toCharArray(), N.toCharArray());
}
}
 
// This code is contributed by shikhasingrajput.


Python3
# python 3 program for the above approach
 
# Function to modify a given string
# by adding ASCII value of characters
# from a string S to integer values of
# same indexed characters in string N
def addASCII(S, N):
 
    # Traverse the string
    for i in range(len(S)):
 
        # Stores integer value of
        # character in string N
        a = ord(N[i]) - ord('0')
 
        # Stores ASCII value of
        # character in string S
        b = ord(S[i]) + a
 
        # If b exceeds 122
        if (b > 122):
            b -= 26
 
        # Replace the character
        S = S.replace(S[i], chr(b))
 
    # Print resultant string
    print(S)
 
# Driver Code
if __name__ == "__main__":
 
    # Given strings
    S = "sun"
    N = "966"
 
    # Function call to modify
    # string S by given operations
    addASCII(S, N)
 
    # This code is contributed by ukasp.


C#
// C# program for the above approach
using System;
public class GFG
{
 
// Function to modify a given String
// by adding ASCII value of characters
// from a String S to integer values of
// same indexed characters in String N
static void addASCII(char []S, char []N)
{
   
    // Traverse the String
    for (int i = 0; i < S.Length; i++)
    {
 
        // Stores integer value of
        // character in String N
        int a = (int)(N[i]) - '0';
 
        // Stores ASCII value of
        // character in String S
        int b = (int)(S[i]) + a;
 
        // If b exceeds 122
        if (b > 122)
            b -= 26;
 
        // Replace the character
        S[i] = (char)(b);
    }
 
    // Print resultant String
    Console.Write(S);
}
 
// Driver Code
public static void Main(String[] args)
{
   
    // Given Strings
    String S = "sun", N = "966";
 
    // Function call to modify
    // String S by given operations
    addASCII(S.ToCharArray(), N.ToCharArray());
}
}
 
// This code is contributed by shikhasingrajput


Javascript


输出:
bat

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live