Skip to content

Closes #7134 : Added a new entry for swap() method under strings in C++. #7154

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions content/cpp/concepts/strings/terms/swap/swap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
---
Title: '.swap()'
Description: 'Exchanges the contents of two strings.'
Subjects:
- 'Computer Science'
- 'Game Development'
Tags:
- 'Strings'
- 'Functions'
- 'Methods'
CatalogContent:
- 'learn-c-plus-plus'
- 'paths/computer-science'
---

The **`.swap()`** method for strings is used to exchange the contents of two strings efficiently.

## Syntax

```pseudo
string1.swap(string2);
```

After calling the `.swap()` method, the contents of `string1` and `string2` are exchanged: `string1` now holds the original contents of `string2`, and `string2` holds the original contents of `string1`.

## Example

Let us consider an example where we have 2 strings - `a` and `b` with the following original values before the `a.swap(b)` method is called :
string a = "apple";
string b = "banana";

After the `a.swap(b)` method is called , the values of strings `a` and `b` are swapped and the strings now hold the following new values :
string a = "banana";
string b = "apple";

## Codebyte Example

In the example below, `.swap()` is called on the `place1` and `place2` strings:

```codebyte/cpp
#include <iostream>
#include <string>

int main() {

std::string place1 = "Los Angeles";
std::string place2 = "New York";

std::cout << "Before swap:\n";
std::cout << "Place 1 : " << place1 << "\n";
std::cout << "Place 2 : " << place2 << "\n";

place1.swap(place2);

std::cout << "After swap:\n";
std::cout << "Place 1 : " << place1 << "\n";
std::cout << "Place 2 : " << place2 << "\n";

return 0;
}
```