Given a string **str** and a positive integer **N**, the task is to reverse **N** characters and skip **N** characters until the end of the string to generate the encrypted message.

**Examples:**

Input:str = “ihTs suohld ebeas!y”, K = 3

Output:This should be easy!

Explanation:

Reverse “ihT” -> “Thi”

“s” remains the same

“uoh” -> “hou”, and so on.

Input:str = “!ysae eb dluohs sihT”, K = 30

Output:This should be easy!

Explanation:

Since 30 is larger than the length of the given string(= 20), just print the reverse of the string.

**Approach:** Follow the steps below to solve the problem:

- Traverse the given string.
- Increment the iterator by
**2 * N**. - Reverse
**N**characters step by step.

Below is the implementation of the above approach:

## C++

`// C++ Program to implement ` `// the above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to decrypt and print the ` `// original strings ` `int` `decryptString(string s, unsigned ` `int` `N) ` `{ ` ` ` ` ` `for` `(unsigned ` `int` `i = 0; i < s.size(); ` ` ` `i += 2 * N) { ` ` ` `auto` `end = s.begin() + i + N; ` ` ` ` ` `// If length is exceeded ` ` ` `if` `(i + N > s.size()) ` ` ` `end = s.end(); ` ` ` ` ` `// Reverse the string ` ` ` `reverse(s.begin() + i, end); ` ` ` `} ` ` ` ` ` `cout << s << endl; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `string s = ` `"ihTs suohld ebeas!y"` `; ` ` ` `unsigned ` `int` `N = 3; ` ` ` `decryptString(s, N); ` ` ` ` ` `return` `0; ` `}` |

*chevron_right*

*filter_none*

**Output:**

This should be easy!

**
Time Complexity:** O(N)

**Auxiliary Space:**O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Decrypt a string encrypted by repeating i-th character i times
- Program to find the Encrypted word
- Decode a median string to the original string
- Count of ordered triplets (R, G, B) in a given original string
- Count of Distinct strings possible by inserting K characters in the original string
- Print n smallest elements from given array in their original order
- Find k maximum elements of array in original order
- Program to calculate GST from original and net prices
- Lexicographically smallest permutation with no digits at Original Index
- Rearrange an Array such that Sum of same-indexed subsets differ from their Sum in the original Array
- Minimum number of given operations required to convert a string to another string
- Minimum given operations required to convert a given binary string to all 1's
- Final string after performing given operations
- Encrypt the given string with the following operations
- Maximum number of given operations to remove the entire string
- Minimum operations required to make the string satisfy the given condition
- Minimum number of operations required to obtain a given Binary String
- Minimum operations required to convert all characters of a String to a given Character
- Find value after N operations to remove N characters of string S with given constraints
- Minimum operations to transform given string to another by moving characters to front or end

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.