/*9.3 编制程序,调用传递引用的参数,实现两个字符串变量的交换。例如开始:
char *ap=”hello”;
char *bp=”how are you?”;
交换的结果使得ap和bp指向的内容分别为:
ap:”how are you?”
bp:”hello”
*/
#include<iostream>
#include<string>
#include<math.h>
using namespace std;
void swap(char *&x,char *&y){
char *temp;
temp = x;
x = y;
y = temp;
}
void main() {
char *ap= "hello";
char *bp = "how are you?";
cout << "*ap=" << ap << endl;
cout << "*bp=" << bp << endl;
swap(*ap,*bp);
cout << "交换后为:" << endl;
cout << "*ap=" <<ap<< endl;
cout << "*bp=" <<bp<< endl;
}
swap(ap,bp),把*号去掉啊。参数要求是指针,你加 * 号后就是字符串的第一个字符了。
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!