自定义交换函数TwoStr(),实现两个字符串的交 换
时间: 2023-05-23 17:01:32 浏览: 37
这是一个简单的交换字符串的函数实现:
```python
def TwoStr(str1, str2):
temp = str1
str1 = str2
str2 = temp
return str1, str2
```
你可以将需要交换的字符串传入函数中,函数会将两个字符串交换并返回。例如:
```python
a = "hello"
b = "world"
a, b = TwoStr(a, b)
print(a, b) # 输出:"world hello"
```
相关问题
函数定义:自定义交换函数TwoStr(),实现两个字符串的交 换
Sure! Here's the code for the TwoStr() swapping function in Lua:
function TwoStr(str1, str2)
return str2, str1
end
You can call this function with two string arguments, and it will return the arguments in swapped order.
For example:
str1 = "Hello"
str2 = "World"
str1, str2 = TwoStr(str1, str2)
print(str1, str2) -- Output: "World Hello"
:自定义交换函数TwoStr(),实现两个字符串的交换。 要求: (1)本次实验中,对字符串的处理暂不使用字符串类! (2)参数传递方式:引用传递。
#include <iostream>
using namespace std;
// 自定义交换函数TwoStr()
void TwoStr(string& str1, string& str2) {
string temp = str1;
str1 = str2;
str2 = temp;
}
int main() {
string str1 = "Hello";
string str2 = "World";
cout << "Before swapping: " << str1 << " " << str2 << endl;
TwoStr(str1, str2);
cout << "After swapping: " << str1 << " " << str2 << endl;
return 0;
}
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)