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.

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 are the codes implementation in various programming languages −

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char* modifyString(char* S) {
   char* res = (char*)malloc(strlen(S) * sizeof(char));
   char** words = (char**)malloc(strlen(S) * sizeof(char*));
   int words_count = 0;

   // Tokenize the string into individual words
   char* word = strtok(S, " ");
   while (word != NULL) {
      words[words_count] = (char*)malloc(strlen(word) * sizeof(char));
      strcpy(words[words_count], word);
      words_count++;
      word = strtok(NULL, " ");
   }
    
   // Iterate over each word
   for (int i = 0; i < words_count; i++) {
      char* word = words[i];
      char* modified_word = (char*)malloc(strlen(word) * sizeof(char));
        
      // Iterate over each character in the word
      for (int j = 0; j < strlen(word); j++) {
         int ascii_value = word[j] + (strlen(word) - 1 - j);
         modified_word[j] = (char)ascii_value;
      }
        
      // Add the modified word to the final string
      strcat(res, modified_word);
      
      // Add a space to the final string if there are more words to be added
      if (i != words_count - 1) {
         strcat(res, " ");
      }
   }

   free(words);
   return res;
}
int main() {
   char S[] = "hello world";
   char* modified_S = modifyString(S);
   printf("%s\n", modified_S);
   free(modified_S);
   return 0;
}

Output

lhnmo {rtmd
#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
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {
   public static String modifyString(String S) {
      String res = "";
      List<String> words = new ArrayList<>();

      // Tokenize the string into individual words
      Scanner ss = new Scanner(S);
      while (ss.hasNext()) {
         String word = ss.next();
         words.add(word);
      }
         
      // Iterate over each word
      for (int i = 0; i < words.size(); i++) {
         String word = words.get(i);
         String modified_word = "";
           
         // Iterate over each character in the word
         for (int j = 0; j < word.length(); j++) {
              
            int ascii_value = word.charAt(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;
   }

   public static void main(String[] args) {
      String S = "hello world";
      String modified_S = modifyString(S);
      System.out.println(modified_S);    // Output
   }
}

Output

lhnmo {rtmd
def modifyString(S):
   res = ""
   words = []

   # Tokenize the string into individual words
   words = S.split()

   # Iterate over each word
   for word in words:
      modified_word = ""
      # Iterate over each character in the word
      for j in range(len(word)):
         ascii_value = ord(word[j]) + (len(word) - 1 - j)
         modified_word += chr(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 word != words[-1]:
         res += " "

   return res


S = "hello world"
modified_S = modifyString(S)
print(modified_S)  

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: 27-Oct-2023

134 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements