# Modify characters of a string by adding integer values of same-indexed characters from another given string

In this problem, we need to modify the given string by adding the value of the digit from the num string to the ASCII value of str’s character.

To solve the problem, we can convert the digit character to the actual digit and add it to the ASCII value of the character. If the ASCII value becomes greater than 122, we start again from 97.

Problem statement – We have given two strings of the same length equal to N. The first string, named str, contains the lowercase alphabetical characters, and the second string, named num, contains digits only. We need to generate a new string after adding the integer value of the digit from the string num to the character of the string str’s ASCII value. Also, we need to do cyclic increments.

## Sample examples

Input– str = "abcd", num = "4444"

Output– ‘efgh’

Explanation

• The ASCII value of ‘a’ is 97. So, 97 + 4 is 101, equivalent to the ASCII value of ‘e’.

• The ASCII value of ‘b’ is 98. So, 98 + 4 is 102, equivalent to the ASCII value of ‘f’.

• The ASCII value of ‘c’ is 99. So, 99 + 4 is 103, equivalent to the ASCII value of ‘g’.

• The ASCII value of ‘d’ is 100. So, 100 + 4 is 104, equivalent to the ASCII value of ‘h’.

Input– str = "Tutorialspoint", num = "26547658932432"

Output– ‘vaysyoftbsqmqv’

Explanation– We added the value of the digit from the num string to the ASCII value of the str string’s character and modified the string.

Input– str = "xyz", num = "321"

Output– ‘aaa’

Explanation

• x’ + 3 = ‘a’

• ‘y’ + 2 = ‘a’

• ‘z’ + 1 = ‘a

## Approach 1

In this approach, we will traverse the string. After that, we will extract the digit value from the ith index of the ‘num’ string and add it to the ASCII value of the character at the ith index in str.

Also, we will do a cyclic sum so that we get valid alphabetical characters.

### Algorithm

• Use the loop to traverse the string.

• Access the ith character from the num string and subtract the ‘0’ from it to convert the character to the digit.

• Access the character at the ith index in the str string, and convert it to a digit to get the ASCII value. After that, add a digit value and store it in the ‘updated’ variable.

• If ‘update’ is greater than 122, decrease the value by 26 to get a valid alphabetical character. Here, 122 is the ASCII code of ‘z’

• Use the char() constructor to convert ‘updated’ to a character and modify the value of the ith index in the str string.

• Return the ‘str’ string.

### Example

#include <bits/stdc++.h>
using namespace std;

// function to change the string by adding digits in the ASCII value of characters
string addASCII(string str, string num) {
// Traverse the string
for (int i = 0; i < str.size(); i++) {
// get the integer value of the character
int digit = int(num[i]) - '0';
// get the updated ASCII value of the character
int updated = int(str[i]) + digit;
// If the update exceeds 122
if (updated > 122)
updated -= 26;
// Replace the character
str[i] = char(updated);
}
// return string
return str;
}
int main() {
string str = "Tutorialspoint", num = "26547658932432";
cout << "The given string after modifying is " << addASCII(str, num);
return 0;
}


### Output

The given string after modifying is Vaysyoftbsqmqv


Time complexity– O(N), as we iterate the string.

Space complexity – O(1), as we modify the same string.

In this tutorial, we learned to modify each character of the string by adding the digit value to the character’s ASCII value. Programmers can also try to modify the string by subtracting the digit value from the ASCII value of each character.

Updated on: 18-Aug-2023

38 Views