C++ Program to Swapping Pair of Characters


A string is a group of characters. They can be described as character arrays as well. An array of characters can be thought of as strings, and each string has a set of indices and values. The switching of characters at two specified indices in a string is one of the modifications we can sometimes make to strings. In this article, we will see how to swap two characters in a string from two given indices using C++.

Syntax

char temp = String_variable[ <first index> ]
String_variable[ <first index> ] = String_variable[ <second index> ]
String_variable[ <second index> ] = temp

Using indices, we may access the characters in a string in C++. To replace a character in this case with another character at a certain index, we simply assign the new character to that position as shown by the syntax. Similar to this, the exchange also took place. We are replacing the first two characters, adding temp to the first position, and then copying a character from the first index to a variable called temp. Let's look at the algorithm to help us comprehend.

Algorithm

  • Take the string s, two indices i and j
  • if the index i and j both are positive and their values are not exceeding the string size, then
    • temp := s[ i ]
    • s[ i ] = s[ j ]
    • s[ j ] = temp
    • return s
  • otherwise
    • return s without changing anything
  • end if

Example

#include <iostream>
using namespace std;
string solve( string s, int ind_first, int ind_second){
   if( (ind_first >= 0 && ind_first < s.length()) && (ind_second >= 0 && ind_second < s.length()) ) {
      char temp = s[ ind_first ];
      s[ ind_first ] = s[ ind_second ];
      s[ ind_second ] = temp;
      return s;
   } else {
      return s;
   }
}
int main(){
   string s = "A string to check character swapping";
   cout << "Given String: " << s << endl;
   cout << "Swap the 6th character and the 12th character." << endl;
   s = solve( s, 6, 12 );
   cout << "\nUpdated String: " << s << endl;
   s = "A B C D E F G H";
   cout << "Given String: " << s << endl;
   cout << "Swap the 4th character and the 10th character." << endl;
   s = solve( s, 4, 10 );
   cout << "Updated String: " << s << endl;
}

Output

Given String: A string to check character swapping
Swap the 6th character and the 12th character.
Updated String: A stricg to nheck character swapping
Given String: A B C D E F G H
Swap the 4th character and the 10th character.
Updated String: A B F D E C G H

Conclusion

In C++, replacing characters at a given index is rather straightforward. This method also allows for character switching. We can directly alter C++ strings because they are changeable. The strings are not mutable in several other programming languages, such as Java. It is not possible to assign a new character to replace one that is already there. In these circumstances, a fresh string must be made. If we define strings as character pointers, as in C, something similar will take place. We have built a function in this example to swap two characters starting at a certain index point. The string will be returned and left unaltered if the specified indices are out of bounds.

Updated on: 14-Dec-2022

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements