Home  >  Article  >  Backend Development  >  C++ program to swap a pair of characters

C++ program to swap a pair of characters

PHPz
PHPzforward
2023-09-11 19:13:021110browse

C++ program to swap a pair of characters

A string is a set of characters. They can also be described as character arrays. an array Characters can be thought of as strings, each string has a set of indices and values Character switching at two specified indexes in the string is one of our modifications Sometimes the string can be changed. In this article we will see how to swap two characters in a Extract a string from given two indices using C.

grammar

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

Using indexes, we can access characters in strings in C. Replace one of the characters In case a character differs from another at some index, we simply assign the new character to that position The location is as shown in the syntax. Similarly, communication takes place. we are Replace the first two characters, add temp to the first position, and copy a Character from the first index into the variable named temp. Let’s look at the algorithm to help us understand.

algorithm

  • Get the string s, two indices i and j
  • If indexes i and j are both positive and their values ​​do not exceed the size of the string, then
    • Temporary := s[ i ]​​i>
    • s[ i ] = s[ j ]​​i>
    • s[ j ] = temperature
    • return
  • otherwise
    • Return s without making any changes
  • 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

in conclusion

In C, replacing a character at a given index is fairly simple. This method is also Allow character switching. We can directly change C strings because they are changeable. Strings are immutable in several other programming languages, e.g. Java. New characters cannot be allocated to replace existing characters. In these Depending on the situation, a new string must be created. If we define string as character pointer like this C. Similar things will happen. We built a function in this example to swap two Character starting from some index point. The string will be returned unchanged if: Character starting from a specific index point. If the string will be returned unchanged The specified index is out of range.

The above is the detailed content of C++ program to swap a pair of characters. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete
Previous article:Find the nth lucky numberNext article:Find the nth lucky number