Open In App

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

Improve
Improve
Like Article
Like
Save
Share
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>


Output

bat

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



Last Updated : 07 Jan, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads