
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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.
- Related Articles
- Golang program to swapping pair of characters
- Swapping Characters of a String in C#
- Java Program to Swap Pair of Characters
- Program to equal two strings of same length by swapping characters in Python
- Recursive program to insert a star between pair of identical characters in C++
- Swapping Characters of a String in Java
- Program to find longest number of 1s after swapping one pair of bits in Python
- Maximum length of balanced string after swapping and removal of characters in C++
- Program to check two strings can be equal by swapping characters or not in Python
- Find a pair of elements swapping which makes sum of two arrays same in C++
- JavaScript program for Swapping Nodes in A Linked List Without Swapping Data
- C# Program to remove duplicate characters from String
- Program to maximize the number of equivalent pairs after swapping in Python
- C++ Program to Find Closest Pair of Points in an Array
- Swapping of subranges from different containers in C++
