Modify string by increasing each character by its distance from the end of the word


When working with strings, sometimes we need to modify them in specific ways to meet certain requirements. One such requirement is to modify a string by increasing each character by its distance from the end of the word. In this article, we will discuss an approach to solving this problem using C++.

Problem Statement

Given a string S, modify the string by increasing each character by its distance from the end of the word.

Approach

To solve this problem, we can follow the following steps −

  • Tokenize the given string S into individual words.

  • Iterate over each word and for each character in the word, add its position from the end to its ASCII value.

  • Add the modified word to the final string, say res.

  • Repeat steps 2 and 3 for all words in the string.

  • Return the final modified string.

Example

Here's the code implementation in C++ −

#include <iostream>
#include <sstream>
#include <vector>

using namespace std;

string modifyString(string S) {
   string res = "";
   vector<string> words;
   
   // Tokenize the string into individual words
   istringstream ss(S);
   string word;
   while (ss >> word) {
      words.push_back(word);
   }
    
   // Iterate over each word
   for (int i = 0; i < words.size(); i++) {
      string word = words[i];
      string modified_word = "";
      
      // Iterate over each character in the word
      for (int j = 0; j < word.length(); j++) {
         int ascii_value = word[j] + (word.length() - 1 - j);
         modified_word += char(ascii_value);
      }
      
      // Add the modified word to the final string
      res += modified_word;
      
      // Add a space to the final string if there are more words to be added
      if (i != words.size() - 1) {
         res += " ";
      }
   }
    
   return res;
}

int main() {
   string S = "hello world";
   string modified_S = modifyString(S);
   cout << modified_S << endl; // Outputs "oekmo kmlqx"
   return 0;
}

Output

lhnmo {rtmd

Time Complexity

The time complexity of the solution is O(N*M), where N is the number of words in the string and M is the average length of a word.

Space Complexity

The space complexity of the solution is O(N*M), where N is the number of words in the string and M is the average length of a word.

In the above example, we have taken the string "hello world" as input. The modified string is "oekmo kmlqx". In the modified string, the first character 'h' has been modified to 'o' as its distance from the end of the word is 4. Similarly, other characters have been modified.

The code implementation first tokenizes the given string S into individual words and stores them in a vector. Then, it iterates over each word and for each character in the word, it adds its position from the end to its ASCII value. This modified word is then added to the final string, res. Finally, the code returns the modified string.

Conclusion

In conclusion, we have successfully modified a given string by increasing each character by its distance from the end of the word. The above approach and implementation can be used to solve similar problems related to string manipulation.

Updated on: 18-May-2023

84 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements