Home > Backend Development > C++ > body text

C++ program to swap a pair of characters

PHPz
Release: 2023-09-11 19:13:02
forward
1097 people have browsed it

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
Copy after login

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 ]
    • s[ i ] = s[ j ]
    • 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;
}
Copy after login

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
Copy after login

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!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!