Given a string str and an integer k, the task is to reverse alternate k characters of the given string. If characters present are less than k, leave them as it is.
Examples:
Input: str = “geeksforgeeks”, k = 3
Output: eegksfgroeeksInput: str = “abcde”, k = 2
Output: bacde
Approach: The idea is to first reverse k characters, then jump onto the next k characters by adding 2 * k to the index and so on until the complete string is modified.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to return the string after // reversing the alternate k characters string revAlternateK(string s, int k, int len) { for (int i = 0; i < s.size();) { // If there are less than k characters // starting from the current position if (i + k > len) break; // Reverse first k characters reverse(s.begin() + i, s.begin() + i + k); // Skip the next k characters i += 2 * k; } return s; } // Driver code int main() { string s = "geeksforgeeks"; int len = s.length(); int k = 3; cout << revAlternateK(s, k, len); return 0; } |
Java
// Java implementation of the approach class GFG { // Function to return the string after // reversing the alternate k characters static String revAlternateK(String s, int k, int len) { for (int i = 0; i < s.length();) { // If there are less than k characters // starting from the current position if (i + k > len) break; // Reverse first k characters s = s.substring(0, i) + new String(new StringBuilder( s.substring(i, i + k)).reverse()) + s.substring(i + k); // Skip the next k characters i += 2 * k; } return s; } // Driver code public static void main(String[] args) { String s = "geeksforgeeks"; int len = s.length(); int k = 3; System.out.println(revAlternateK(s, k, len)); } } // This code is contributed by 29AjayKumar |
Python3
# Python3 implementation of the approach # Function to return the string after # reversing the alternate k characters def revAlternateK(s, k, Len): i = 0 while(i < len(s)): # If there are less than k characters # starting from the current position if (i + k > Len): break # Reverse first k characters ss = s[i:i + k] s = s[:i]+ss[::-1]+s[i + k:] # Skip the next k characters i += 2 * k return s; # Driver code s = "geeksforgeeks"Len = len(s) k = 3print(revAlternateK(s, k, Len)) # This code is contributed by mohit kumar |
C#
// C# implementation of the approach using System; class GFG { // Function to return the string after // reversing the alternate k characters static String revAlternateK(String s, int k, int len) { for (int i = 0; i < s.Length;) { // If there are less than k characters // starting from the current position if (i + k > len) break; // Reverse first k characters s = s.Substring(0, i) + reverse(s.Substring(i, k).ToCharArray(), 0, k - 1) + s.Substring(i + k); // Skip the next k characters i += 2 * k; } return s; } static String reverse(char []str, int start, int end) { // Temporary variable to store character char temp; while (start <= end) { // Swapping the first and last character temp = str[start]; str[start] = str[end]; str[end] = temp; start++; end--; } return String.Join("", str); } // Driver code public static void Main(String[] args) { String s = "geeksforgeeks"; int len = s.Length; int k = 3; Console.WriteLine(revAlternateK(s, k, len)); } } // This code contributed by Rajput-Ji |
eegksfgroeeks
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:
- Minimum number of characters to be removed to make a binary string alternate
- Reverse alternate K nodes in a Singly Linked List
- Given a linked list, reverse alternate nodes and append at the end
- Reverse alternate levels of a perfect binary tree using Stack
- Reverse alternate levels of a perfect binary tree
- Create a new string by alternately combining the characters of two halves of the string in reverse
- Reverse a string without affecting special characters
- Add index to characters and reverse the string
- Min flips of continuous characters to make all characters same in a string
- String with k distinct characters and no same characters adjacent
- Permutation of a string with maximum number of characters greater than its adjacent characters
- Rearrange the characters of the string such that no two adjacent characters are consecutive English alphabets
- Count of ungrouped characters after dividing a string into K groups of distinct characters
- Minimum cost to remove the spaces between characters of a String by rearranging the characters
- Subsequences generated by including characters or ASCII value of characters of given string
- Alternate vowel and consonant string
- Print the string by ignoring alternate occurrences of any character
- Lexicographically first alternate vowel and consonant string
- Rearrange a binary string as alternate x and y occurrences
- Check if it is possible to rearrange a binary string with alternate 0s and 1s
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.

