Modify characters of a string by adding integer values of same-indexed characters from another given string Last Updated : 23 Jul, 2025 Comments Improve Suggest changes Like Article Like 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) Comment More infoAdvertise with us Next Article Data Structures Tutorial S subham348 Follow Improve Article Tags : Strings DSA ASCII Practice Tags : Strings Similar Reads DSA Tutorial - Learn Data Structures and Algorithms DSA (Data Structures and Algorithms) is the study of organizing data efficiently using data structures like arrays, stacks, and trees, paired with step-by-step procedures (or algorithms) to solve problems effectively. Data structures manage how data is stored and accessed, while algorithms focus on 7 min read Data Structures Tutorial Data structures are the fundamental building blocks of computer programming. They define how data is organized, stored, and manipulated within a program. Understanding data structures is very important for developing efficient and effective algorithms. What is Data Structure?A data structure is a st 2 min read Merge Sort - Data Structure and Algorithms Tutorials Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. Merge 12 min read Quick Sort QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. It works on the principle of divide and conquer, breaking down the problem into s 12 min read Bubble Sort Algorithm Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high.We sort the array using multiple passes. After the fir 8 min read Binary Search Algorithm - Iterative and Recursive Implementation Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N). Binary Search AlgorithmConditions to apply Binary Searc 15 min read Breadth First Search or BFS for a Graph Given a undirected graph represented by an adjacency list adj, where each adj[i] represents the list of vertices connected to vertex i. Perform a Breadth First Search (BFS) traversal starting from vertex 0, visiting vertices from left to right according to the adjacency list, and return a list conta 15+ min read Linked List Data Structure A linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List: 2 min read Insertion Sort Algorithm Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an unsorted list into its correct position in a sorted portion of the list. It is like sorting playing cards in your hands. You split the cards into two groups: the sorted cards and the unsorted cards. T 9 min read Array Data Structure Guide In this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous 3 min read Like