引用为参数实现两个字符串变量的交换
2008-03-31 15:59
363 查看
如题所示,通过调用传递引用的参数,实现两个字符串变量的交换,例如:
char * ap="hello";
char * bp="how are you";
交换的结果使得ap和bp指向的内容分别为:
char * ap="how are you"; ="hello";
char * bp="hello";
好的,下面开始代码:
#include <iostream.h>
void swapstring(char * & ca,char * & cb)
{
char * temp;
temp=ca;
ca=cb;
cb=temp;
}
void main(int argc, char* argv[])
{
char * ap="hello";
char * bp="how are you";
cout<<"ap: "<<ap<<endl;
cout<<"bp: "<<bp<<endl;
swapstring(ap,bp);
cout<<"after swapping..."<<endl;
cout<<"ap: "<<ap<<endl;
cout<<"bp: "<<bp<<endl;
}
char * ap="hello";
char * bp="how are you";
交换的结果使得ap和bp指向的内容分别为:
char * ap="how are you"; ="hello";
char * bp="hello";
好的,下面开始代码:
#include <iostream.h>
void swapstring(char * & ca,char * & cb)
{
char * temp;
temp=ca;
ca=cb;
cb=temp;
}
void main(int argc, char* argv[])
{
char * ap="hello";
char * bp="how are you";
cout<<"ap: "<<ap<<endl;
cout<<"bp: "<<bp<<endl;
swapstring(ap,bp);
cout<<"after swapping..."<<endl;
cout<<"ap: "<<ap<<endl;
cout<<"bp: "<<bp<<endl;
}
相关文章推荐
- 不借助第三个参数实现两个变量的值互相交换
- 每日一题:不适用第三个变量,实现交换两个输入参数
- 每日一题:不适用第三个变量,实现交换两个输入参数
- 交换两个变量的值实现方法,及容易出错之处
- C/C++学习之C提高----C数组的定义、类型、定义数组指针变量、多维数组的本质、多维数组做函数参数、将两个数组中的字符串copy到第三个数组
- 实现两个变量的值互换,用“引用”做实参
- 不使用第三个参数和库函数实现两个数字的交换(异或的妙用)
- 不借助第三个变量实现两个变量交换的思考
- 编写函数实现对两个变量的值进行交换
- 基于JAVA的不借助第三个变量实现交换两个变量的值的小代码
- 不用中间变量,实现两个变量的交换
- 不借助第三个变量实现两个变量的交换
- Day5_实现两个变量数据的交换
- C语言基础—实现两个变量交换的三种方法
- Python实现:斐波那契数列、交换两个变量的值、从一组数据中找出最大最小值
- 不使用第三方变量交换两个参数的值
- 在编程中实现两个变量的值交换
- 使用指针引用的方法和二维指针的方法交换两个字符串
- 特殊运算符实现 两个变量交换
- JAVA不借助第三个变量实现两个变量交换的思考