Replace every character of string by character whose ASCII value is K times more than it


In the realm of C++ programming, it is possible to replace every character of a specified string with a symbol whose ASCII value is elevated by a factor of K compared to the original character. This can be accomplished through the implementation of a straightforward algorithmic technique. This piece delves into the syntax, the algorithm, and two distinct methods to address the problem, complete with code illustrations in C++.

Syntax

To substitute every character in a string with a character whose ASCII value is multiplied by K, the syntax is as follows −

string replace(string str, int K);

Here, "str" represents the string that requires modification, while "K" stands for the integer value that the ASCII value of each character will be multiplied by.

Algorithm

The algorithmic procedure to substitute every character of a string with a character whose ASCII value has been multiplied by K is as follows −

  • Start

  • Input value of0020string "str" and the integer value "K."

  • Navigate through each character of "str" by utilizing a loop.

  • For each character, calculate its ASCII representation by converting it into a numerical value through typecasting using (int)str[i].

  • Multiply the ASCII value with "K" to determine the new ASCII value.

  • Substitute the character with the new character obtained by converting the new ASCII value back to a character through typecasting using (char)(ASCII value).

  • Repeat steps 3 through 6 for each character present in the string.

  • End

Approach 1

The first approach entails the utilization of a "for" loop to traverse through each character of the string and execute the necessary operations on each symbol. The code for this approach is as follows −

Example

At the outset, we determine the extent of the input sequence "str" by computing its length. Then, through the implementation of a "for" loop, we traverse each individual element of the string. For each character, we calculate its ASCII equivalent by transforming it into a numerical representation through typecasting using (int)str[i]. Subsequently, we multiply this ASCII value with a constant "K" to arrive at a new ASCII value. Finally, we replace the character with a new symbol obtained by converting the new ASCII value back to its character form through typecasting using (char)(ASCII value). This process is repeated for each character within the string, yielding the modified string as the final outcome.

#include <iostream>
#include <string>

using namespace std;

string replace(string str, int K){
   int len = str.length();
   for(int i=0; i<len; i++){
      int ascii = (int)str[i];
      int new_ascii = ascii * K;
      str[i] = (char)new_ascii;
   }
   return str;
}

int main(){
   string str = "hello";
   int K = 3;

   string new_str = replace(str, K);
   cout<<"Modified string: "<<new_str<<endl;
   return 0;
}

Output

Modified string: 8/DDM

Approach 2

The alternative approach involves utilizing the "replace()" function of the string class to carry out the required operations on each character. The code for this technique is outlined below −

Example

Initially, we calculate the length of the input string "str." Then, employing a "for" loop, we traverse through each character of the string. For each symbol, we compute its ASCII value by transforming it into an integer value through typecasting using (int)str[i]. We multiply the ASCII value with "K" to determine the new ASCII value. We create a new string named "new_char" and add the new symbol obtained by converting the new ASCII value back to a character through typecasting using (char)(ASCII value). We then utilize the "replace()" function of the string class to substitute the character located at index "i" with the new symbol "new_char." These steps are repeated for each character in the string. The modified string is then returned as the final result.

#include <iostream>
#include <string>

using namespace std;

string replace(string str, int K){
   int len = str.length();
   for(int i=0; i<len; i++){
      int ascii = (int)str[i];
      int new_ascii = ascii * K;
      string new_char = "";
      new_char += (char)new_ascii;
      str.replace(i, 1, new_char);
   }
   return str;
}

int main(){
   string str = "helloworld";
   int K = 3;
   string new_str = replace(str, K);

   cout<<"Modified string: "<<new_str<<endl;
   return 0;
}

Output

Modified string: 8/DDMeMVD,

Conclusion

In this article, we explored the method of replacing each character in a string with a symbol whose ASCII value has been multiplied by K in C++. Two diverse approaches to address this issue were presented, accompanied by code examples for both methods. This problem can prove to be valuable in situations where it is necessary to alter a given string based on some arithmetic operation applied to its ASCII values.

Updated on: 21-Jul-2023

358 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements