c++指针学习笔记--交换两个字符串数据

本文介绍五种不同的方式来实现两个字符串的交换操作,包括使用字符指针、字符数组、标准字符串类型、引用以及指针引用等方法,并提供了详细的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

交换两个字符串数据。
一:字符指针

交换两个字符串数据。
一:字符指针

   3. int main()
   4. {
   5.     void charsort2(char ** ,char **);
   6.         char * s1="abc";
   7.     char * s2="baihe";  
   8.     charsort2 (&s1,&s2);
   9.     cout<<s1<<endl;
  10.     cout<<s2<<endl;    
  11.     return 0;   
  12. }
  13. void charsort2(char **s1,char **s2)
  14. {
  15.     char *p;
  16.     p=*s1;
  17.     *s1=*s2;
  18.     *s2=p;
  19. }

二:字符数组

  21. int main()
  22. {
  23.     void charsort( char * ,char * );
  24.         char s1[7]="abc";
  25.     char s2[7]="baihe"; 
  26.     charsort(s1,s2);
  27.     cout<<s1<<endl;
  28.     cout<<s2<<endl; 
  29.     return 0;   
  30. }
  31. void charsort(char *s1,char *s2)
  32. {
  33.     char p[7];
  34.     strcpy(p,s1);
  35.     strcpy(s1,s2);
  36.     strcpy(s2,p);   
  37. }

三:字符串

  39. int main()
  40. {
  41.     void strsort(string *,string *);
  42.         string s1="abc";
  43.     string s2="baihe";  
  44.     strsort(&s1,&s2);
  45.     cout<<s1<<endl;
  46.     cout<<s2<<endl;    
  47.     return 0;   
  48. }
  49. void strsort(string *s1,string *s2)
  50. {
  51.     string p;
  52.     p=*s1;
  53.         *s1=*s2;
  54.     *s2=p;
  55. }

四:引用

  57. int main()
  58. {
  59.     void strsort(string &,string &);
  60.         string s1="abc";
  61.     string s2="baihe";  
  62.     strsort(s1,s2);
  63.     cout<<s1<<endl;
  64.     cout<<s2<<endl;    
  65.     return 0;   
  66. }
  67. void strsort(string & s1,string &s2)
  68. {
  69.     string p;
  70.     p=s1;
  71.     s1=s2;
  72.     s2=p;
  73. }

多谢论坛上朋友的分享.以下为新添方法:

五:指针引用:

  76. int main()
  77. {
  78.     void charsort2(char*& ,char*&);
  79.     char * s1="abc";
  80.     char * s2="baihe";    
  81.     charsort2 (s1,s2);
  82.     cout<<s1<<endl;
  83.     cout<<s2<<endl;    
  84.     return 0;    
  85. }
  86. void charsort2(char *&s1,char *&s2)
  87. {
  88.     char *p;
  89.     p=s1;
  90.     s1=s2;
  91.     s2=p;
  92. }
 

二:字符数组

 

三:字符串

  1. int  main()
  2. {
  3.      void  strsort( string  *, string  *);
  4.          string  s1= "abc" ;
  5.      string  s2= "baihe" ;  
  6.     strsort(&s1,&s2);
  7.     cout<<s1<<endl;
  8.     cout<<s2<<endl;    
  9.      return  0;   
  10. }
  11. void  strsort( string  *s1, string  *s2)
  12. {
  13.      string  p;
  14.     p=*s1;
  15.         *s1=*s2;
  16.     *s2=p;
  17. }

四:引用

  1. int  main()
  2. {
  3.      void  strsort( string  &, string  &);
  4.          string  s1= "abc" ;
  5.      string  s2= "baihe" ;  
  6.     strsort(s1,s2);
  7.     cout<<s1<<endl;
  8.     cout<<s2<<endl;    
  9.      return  0;   
  10. }
  11. void  strsort( string  & s1, string  &s2)
  12. {
  13.      string  p;
  14.     p=s1;
  15.     s1=s2;
  16.     s2=p;
  17. }

多谢论坛上朋友的分享.以下为新添方法:

五:指针引用:

  1. int  main()
  2. {
  3.      void  charsort2( char *& , char *&);
  4.      char  * s1= "abc" ;
  5.      char  * s2= "baihe" ;    
  6.     charsort2 (s1,s2);
  7.     cout<<s1<<endl;
  8.     cout<<s2<<endl;    
  9.      return  0;    
  10. }
  11. void  charsort2( char  *&s1, char  *&s2)
  12. {
  13.      char  *p;
  14.     p=s1;
  15.     s1=s2;
  16.     s2=p;
  17. }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值