Open In App

Modify characters of a string by adding integer values of same-indexed characters from another given string

Improve
Improve
Like Article
Like
Save
Share
Report

Given two strings S and N of the same length, consisting of alphabetical and numeric characters respectively, the task is to generate a new string obtained by adding the integer value of each character of string N with the ASCII value of the same indexed character of string S. Finally, print the resultant string.
Note: If the sum exceeds 122, then subtract 26 from the sum and print the resultant character.

Examples:

Input: S = “sun”, N = “966”
Output: “bat”
Explanation:
ASCII value of ‘s’ = 115.
Therefore, 115 + 9 = 124 – 26 = 98. Therefore, equivalent character is’b’.
ASCII value of ‘u’ = 117.
Therefore, 117 + 6 = 123 – 26 = 97. Therefore, equivalent character is ‘a’.
ASCII value of ‘n’ = 110.
Therefore, 110 + 6 = 116. Therefore, equivalent character is ‘t’.

Input: S = “apple”, N = “12580”
Output: “brute”

Approach: Follow the steps below to solve the problem:

  • Traverse the string S:
    • Convert the current character of string N to its equivalent integer value. 
    • Add the obtained integer value to the equivalent ASCII value of the current character in string S.
    • If the value exceeds 122, which is the ASCII value of the last alphabet ‘z’, then subtract the value by 26.
    • Update string S by replacing a current character with the character whose ASCII value is equal to the value obtained.
  • Print the resultant string after completing the above steps.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
 
#include <bits/stdc++.h>
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




<script>
      // JavaScript 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
      function addASCII(S, N) {
        var newStr = new Array(S.length);
        // Traverse the string
        for (var i = 0; i < S.length; i++) {
          // Stores integer value of
          // character in string N
          var a = N[i].charCodeAt(0) - "0".charCodeAt(0);
 
          // Stores ASCII value of
          // character in string S
          var b = S[i].charCodeAt(0) + a;
 
          // If b exceeds 122
          if (b > 122) b -= 26;
 
          // Replace the character
          newStr[i] = String.fromCharCode(b);
        }
 
        // Print resultant string
        document.write(newStr.join(""));
      }
 
      // Driver Code
      // Given strings
      var S = "sun",
        N = "966";
 
      // Function call to modify
      // string S by given operations
      addASCII(S, N);
    </script>


 
 

Output: 

bat

 

Time Complexity: O(|S|)
Auxiliary Space: O(1) 



Last Updated : 11 May, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads