Generate string by incrementing character of given string by number present at corresponding index of second string - GeeksforGeeks

07 Jan, 2022
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).
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++
Java
Python3
C#
Javascript
// C++ program for the above approach #include <bits/stdc++.h>using namespace std; // Function to update stringstring 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 Codeint main(){    string S = "sun";    string N = "966";    cout << updateStr(S, N);    return 0;}
// Java code to implement above approachimport 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.
# Python code for the above approach # Function to update stringdef 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# code to implement above approachusing 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
<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>
bat
Time Complexity: O(|S|)Auxiliary Space: O(1)
lokeshpotta20
_saurabh_jaiswal
samim2000
shikhasingrajput
gulshankumarar231
ASCII
Mathematical
Strings
Strings
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Program to print prime numbers from 1 to N.
Segment Tree | Set 1 (Sum of given range)
Modular multiplicative inverse
Count all possible paths from top left to bottom right of a mXn matrix
Fizz Buzz Implementation
Write a program to reverse an array or string
Reverse a string in Java
Longest Common Subsequence | DP-4
Check for Balanced Brackets in an expression (well-formedness) using Stack
Python program to check if a string is palindrome or not