Reverse alternate k characters in a string


Introduction

In this tutorial, we implement examples using C++ programming logic to reverse the alternate k characters in an input string. We define the value of k to reverse the characters in a given string. K is the number of characters to be reversed. The value of k can be anything. If the value of k is more than the total number of characters in the string we do not reverse any of the string characters.

Demonstration 1

String = “tutorialspoint” 
K = 4

Output

otutrialiopsnt

In the above demonstration, we consider a string “tutorialspoint” to reverse its alternate 4 characters. The value of k is 4. K =4 means that the first 4 characters are reversed, then next 4 characters are reversed and so on untill the whole string is covered. After reversing the alternate k characters in a given string the output is otutrialiopsnt.

Demonstration 2

String = “hello”
K = 2

Output

ehllo

In the above demonstration, we take the input string “hello”. The value of k is 2. We reverse the alternate 2 characters in the input string. After reversing the output is “ehllo”.

C++ Library Functions

Syntax

length(): It is a string class library function that returns the length of the string. The length of the string in byte format is the sum of the total characters in the string.

string_name.length();

size(): It is a standard library function defined in the <std> header file. It returns the size of the string.

string_name.size();

reverse(): This library function is defined in the Standard Template C++ library. It alters the string character order. In order to reverse the string, it requires two arguments: the starting pointer and the ending pointer of the string.

reverse(string_name.begin(), string_name.end());

Algorithm

  • Take an input string.

  • Define the value of k.

  • Reverse the first k characters of the input string.

  • Skip the next k characters in the string.

  • Reverse the other k characters in the string.

  • Print the reversed string.

Example 1

We implement the above-described problem by considering an input string. First reverse the k characters of the input string then jump to the next alternate k characters using the formula k*2 index value.

#include <bits/stdc++.h>
using namespace std;
 
// user-defined function to reverse the alternate k characters of the string
string reverseString(string str, int k, int l){
 
    for (int x = 0; x < str.size();){
 
        // when the string has characters less the the value of k
        if (x + k > l)
            break;
 
        //reversing the starting characters of k value
        reverse(str.begin() + x, str.begin() + x + k);
 
        // moving in the string for reversing k characters
        x += 2 * k;
    }
    return str;
}
 
// Controller
int main(){
    string str = "tutorialspoint";
    int l = str.length();
    int k = 4;
    cout << "The reversed string is: "<< reverseString(str, k, l);
 
    return 0;
}

Output

The reversed string is: otutrialiopsnt

Example 2

In this implementation, we traverse the input string “tutorialspoint” and define the value of k as 2. Create a user-defined function reverseString() which traverses the string by reversing the k characters and skipping the next k characters. The process continues until the whole string is covered.

#include <bits/stdc++.h>
using namespace std;
 
// User-defined function to reverse alternate k characters
//of the input string
string reverseString(string s, int k, int m){
    string result = "";
 
    int x = 0, y = 0;
 
    // Traversing the string s till the end
    while (x < m){
 
        // Traversing the string till the last character and trading backward
        for (y = min(x + k, m) - 1; y >= x; y--)
            result += s[y];
        x = min(x + k, m);
 
        // Traversing the string for the x to x+k characters 
        for (y = x; y < min(x + k, m); y++)
            result += s[y];
        x = y;
    }
 
    // Returning result
    return result;
}
 
// Controller
int main(){
    string s = "tutorialspoint";
    int M = s.length();
    int K = 2;
    cout << "The reversed string for every alternate k character is : "<< reverseString(s, K, M);
 
    return 0;
}

Output

The reversed string for every alternate k character is : uttoiralpsoitn

Conclusion

We have reached the end of this tutorial. In this tutorial, we implemented two examples to reverse the alternate k characters in the input string. For every implementation, define the value of k. Use the length() and size() functions of the C++ library to find the length of the input string so that it can be traversed.

Demonstrated the task with two input strings and explained how the C++ implementation generates the result.

Updated on: 18-Aug-2023

140 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements