Generate string by incrementing character of given string by number present at corresponding index of second string Last Updated : 07 Jan, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report Given two strings S[] and N[] of the same size, the task is to update string S[] by adding the digit of string N[] of respective indices. Examples: Input: S = "sun", N = "966"Output: bat Input: S = "apple", N = "12580"Output: brute Approach: The idea is to traverse the string S[] from left to right. Get the ASCII value of string N[] and add it to the ASCII value of string S[]. If the value exceeds 122, which is the ASCII value of the last alphabet 'z'. Then subtract the value by 26, which is the total count of English alphabets. Update string S with the character of ASCII value obtained. Follow the steps below to solve the problem: Iterate over the range [0, S.size()) using the variable i and perform the following tasks:Initialize the variables a and b as the integer and ascii value of N[i] and S[i].If b is greater than 122 then subtract 26 from b.Set S[i] as char(b).After performing the above steps, print the value of S[] as the answer. Below is the implementation of the above approach. C++ // C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to update string string updateStr(string S, string N) { for (int i = 0; i < S.size(); i++) { // Get ASCII value int a = int(N[i]) - '0'; int b = int(S[i]) + a; if (b > 122) b -= 26; S[i] = char(b); } return S; } // Driver Code int main() { string S = "sun"; string N = "966"; cout << updateStr(S, N); return 0; } Java // Java code to implement above approach import java.util.*; public class GFG { // Function to update string static String updateStr(String S, String N) { String t = ""; for (int i = 0; i < S.length(); i++) { // Get ASCII value int a = (int)(N.charAt(i) - '0'); int b = (int)(S.charAt(i) + a); if (b > 122) b -= 26; char x = (char)b; t +=x; } return t; } // Driver code public static void main(String args[]) { String S = "sun"; String N = "966"; System.out.println(updateStr(S, N)); } } // This code is contributed by Samim Hossain Mondal. Python3 # Python code for the above approach # Function to update string def updateStr(S, N): S = list(S) for i in range(len(S)): # Get ASCII value a = ord(N[i]) - ord('0') b = ord(S[i]) + a if (b > 122): b -= 26 S[i] = chr(b) return "".join(S) # Driver Code S = "sun" N = "966" print(updateStr(S, N)) # This code is contributed by Saurabh Jaiswal C# // C# code to implement above approach using System; public class GFG { // Function to update string static String updateStr(String S, String N) { String t = ""; for (int i = 0; i < S.Length; i++) { // Get ASCII value int a = (int)(N[i] - '0'); int b = (int)(S[i] + a); if (b > 122) b -= 26; char x = (char)b; t +=x; } return t; } // Driver code public static void Main(String []args) { String S = "sun"; String N = "966"; Console.WriteLine(updateStr(S, N)); } } // This code is contributed by shikhasingrajput JavaScript <script> // JavaScript code for the above approach // Function to update string function updateStr(S, N) { S = S.split('') for (let i = 0; i < S.length; i++) { // Get ASCII value let a = (N[i].charCodeAt(0) - '0'.charCodeAt(0)); let b = (S[i].charCodeAt(0)) + a; if (b > 122) b -= 26; S[i] = String.fromCharCode(b); } return S.join(''); } // Driver Code let S = "sun"; let N = "966"; document.write(updateStr(S, N)); // This code is contributed by Potta Lokesh </script> Outputbat Time Complexity: O(|S|)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Count number of substrings of a string consisting of same characters V vansikasharma1329 Follow Improve Article Tags : Strings Mathematical DSA ASCII Practice Tags : MathematicalStrings Similar Reads Generate a Number in Decreasing order of Frequencies of characters of a given String Given a string Str of length N, consisting of lowercase alphabets, the task is to generate a number in decreasing order of the frequency of characters in the given string. If two characters have the same frequency, the character with a smaller ASCII value appears first. Numbers assigned to character 12 min read Generate two output strings depending upon occurrence of character in input string. Given an input string str[], generate two output strings. One of which consists of those character which occurs only once in input string and second which consists of multi-time occurring characters. Output strings must be sorted.Examples: Input : str[] = "geeksforgeeks"Output : String with characte 7 min read Replace even-indexed characters of minimum number of substrings to convert a string to another Given two strings, str1 and str2 of length N, the task is to convert the string str1 to string str2 by selecting a substring and replacing all characters present at even indices of the substring by any possible characters, even number of times. Examples: Input: str1 = "abcdef", str2 = "ffffff" Outpu 7 min read Generate a Sequence by inserting positions into Array based on corresponding String value Given a string S of length N. The string consists only of letters 'F' and 'B'. The task is to generate a sequence performing some operations such that: Consider an integer sequence A that consists of only a 0, i.e. A = (0).Now, for each index(i) of the string (1 to N), if S[i] is 'F' add i to the im 6 min read Count number of substrings of a string consisting of same characters Given a string. The task is to find out the number of substrings consisting of the same characters. Examples: Input: abba Output: 5 The desired substrings are {a}, {b}, {b}, {a}, {bb} Input: bbbcbb Output: 10 Approach: It is known for a string of length n, there are a total of n*(n+1)/2 number of su 6 min read Modify characters of a string by adding integer values of same-indexed characters from another given string 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 res 6 min read Like