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 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++
#include <bits/stdc++.h>
using namespace std;
void addASCII(string S, string N)
{
for ( int i = 0; i < S.size(); i++) {
int a = int (N[i]) - '0' ;
int b = int (S[i]) + a;
if (b > 122)
b -= 26;
S[i] = char (b);
}
cout << S;
}
int main()
{
string S = "sun" , N = "966" ;
addASCII(S, N);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void addASCII( char []S, char []N)
{
for ( int i = 0 ; i < S.length; i++)
{
int a = ( int )(N[i]) - '0' ;
int b = ( int )(S[i]) + a;
if (b > 122 )
b -= 26 ;
S[i] = ( char )(b);
}
System.out.print(S);
}
public static void main(String[] args)
{
String S = "sun" , N = "966" ;
addASCII(S.toCharArray(), N.toCharArray());
}
}
|
Python3
def addASCII(S, N):
for i in range ( len (S)):
a = ord (N[i]) - ord ( '0' )
b = ord (S[i]) + a
if (b > 122 ):
b - = 26
S = S.replace(S[i], chr (b))
print (S)
if __name__ = = "__main__" :
S = "sun"
N = "966"
addASCII(S, N)
|
C#
using System;
public class GFG
{
static void addASCII( char []S, char []N)
{
for ( int i = 0; i < S.Length; i++)
{
int a = ( int )(N[i]) - '0' ;
int b = ( int )(S[i]) + a;
if (b > 122)
b -= 26;
S[i] = ( char )(b);
}
Console.Write(S);
}
public static void Main(String[] args)
{
String S = "sun" , N = "966" ;
addASCII(S.ToCharArray(), N.ToCharArray());
}
}
|
Javascript
<script>
function addASCII(S, N) {
var newStr = new Array(S.length);
for ( var i = 0; i < S.length; i++) {
var a = N[i].charCodeAt(0) - "0" .charCodeAt(0);
var b = S[i].charCodeAt(0) + a;
if (b > 122) b -= 26;
newStr[i] = String.fromCharCode(b);
}
document.write(newStr.join( "" ));
}
var S = "sun" ,
N = "966" ;
addASCII(S, N);
</script>
|
Time Complexity: O(|S|)
Auxiliary Space: O(1)
Last Updated :
11 May, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...