# Add index to characters and reverse the string

Given a string str, the task is to encrypt and reverse the string. The string is encrypted by adding every character of the string with it’s index in the string i.e. if character‘a’ is at index 2 then the character in the updated string will be ‘a’ + 2 = ‘c’. Since value of string may go beyond 256, do the addition under modulo 256.

Examples:

Input: str = “geeks”
Output: wngfg
‘g’ + 0 = ‘g’
‘e’ + 1 = ‘f’
‘e’ + 2 = ‘g’
‘k’ + 3 = ‘n’
‘s’ + 4 = ‘w’

Input: str = “java”
Output: dxbj

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

• Add every character of the string with its index within the same string.
• Reverse the string.
• Print the reversed string.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to return the encryptet string ` `string getEncryptedString(string str) ` `{ ` ` `  `    ``// To build the encrypted string ` `    ``string sb =``""``; ` ` `  `    ``// Traverse the string in reverse ` `    ``for` `(``int` `i = str.length() - 1; i >= 0; i--)  ` `    ``{ ` `        ``sb+=(``char``)((``int``)str[i] + i) % 256; ` `    ``} ` ` `  `    ``// Return the encrypted string ` `    ``return` `sb; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``string str = ``"geeks"``; ` `    ``cout<

## Java

 `// Java implementation of the approach ` `public` `class` `HelloWorld { ` ` `  `    ``// Function to return the encryptet string ` `    ``static` `String getEncryptedString(String str) ` `    ``{ ` ` `  `        ``// To build the encrypted string ` `        ``StringBuilder sb = ``new` `StringBuilder(``""``); ` ` `  `        ``// Traverse the string in reverse ` `        ``for` `(``int` `i = str.length() - ``1``; i >= ``0``; i--) { ` `            ``sb.append(``""` `+ (``char``)((str.charAt(i) + i) % ``256``)); ` `        ``} ` ` `  `        ``// Return the encrypted string ` `        ``return` `sb.toString(); ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``String str = ``"geeks"``; ` `        ``System.out.println(getEncryptedString(str)); ` `    ``} ` `} `

## Python3

 `# Python3 implementation of the approach ` ` `  `# Function to return the encryptet string ` `def` `getEncryptedString(``str``): ` ` `  ` `  `    ``# To build the encrypted string ` `    ``sb ``=` `""; ` ` `  `    ``# Traverse the string in reverse ` `    ``for` `i ``in` `range``(``len``(``str``) ``-` `1``,``-``1``,``-``1``):  ` `        ``sb ``+``=` `chr``((``ord``(``str``[i]) ``+` `i) ``%` `256``); ` ` `  `    ``# Return the encrypted string ` `    ``return` `sb; ` ` `  `# Driver code ` `str` `=` `"geeks"``; ` `print``(getEncryptedString(``str``)); ` ` `  `# This code is contributed by mits `

## C#

 `// C# implementation of the approach ` `using` `System; ` `using` `System.Text;  ` ` `  `public` `class` `HelloWorld  ` `{ ` ` `  `    ``// Function to return the encryptet string ` `    ``static` `String getEncryptedString(String str) ` `    ``{ ` ` `  `        ``// To build the encrypted string ` `        ``StringBuilder sb = ``new` `StringBuilder(``""``); ` ` `  `        ``// Traverse the string in reverse ` `        ``for` `(``int` `i = str.Length - 1; i >= 0; i--)  ` `        ``{ ` `            ``sb.Append(``""` `+ (``char``)((str[i] + i) % 256)); ` `        ``} ` ` `  `        ``// Return the encrypted string ` `        ``return` `sb.ToString(); ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `Main(String[] args) ` `    ``{ ` `        ``String str = ``"geeks"``; ` `        ``Console.WriteLine(getEncryptedString(str)); ` `    ``} ` `} ` ` `  `// This code contributed by Rajput-Ji `

## PHP

 `= 0; ``\$i``--)  ` `    ``{ ` `        ``\$sb` `.= ``chr``((ord(``\$str``[``\$i``]) + ``\$i``) % 256); ` `    ``} ` ` `  `    ``// Return the encrypted string ` `    ``return` `\$sb``; ` `} ` ` `  `// Driver code ` `\$str` `= ``"geeks"``; ` `print``(getEncryptedString(``\$str``)); ` ` `  `// This code is contributed by mits ` `?> `

Output:

```wngfg
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : Rajput-Ji, Mithun Kumar

Article Tags :
Practice Tags :

2

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.