The Wayback Machine - https://web.archive.org/web/20210301162728/https://www.geeksforgeeks.org/modify-characters-of-a-string-by-adding-integer-values-of-same-indexed-characters-from-another-given-string/
Skip to content
Related Articles

Related Articles

Modify characters of a string by adding integer values of same-indexed characters from another given string
  • Last Updated : 01 Mar, 2021

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;
}


Output:

bat

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :