---
title: " c++指针学习笔记--交换两个字符串数据"
author: "dhj"
date: "2008-10-10T10:39:09Z"
tags: []
slug: "c"
draft: false
lastmod: "2008-10-10T10:39:09Z"
id: 251114
url: "https://dang.iteye.com/blog/251114"
---

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

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

   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. }
```

 

二：字符数组

 

三：字符串

01. int  main()
02. {
03. void  strsort( string  \*, string  \*);
04. string  s1= "abc" ;
05. string  s2= "baihe" ;
06. strsort(&s1,&s2);
07. cout&lt;&lt;s1&lt;&lt;endl;
08. cout&lt;&lt;s2&lt;&lt;endl;
09. return  0;
10. }
11. void  strsort( string  \*s1, string  \*s2)
12. {
13. string  p;
14. p=\*s1;
15. \*s1=\*s2;
16. \*s2=p;
17. }

四：引用

01. int  main()
02. {
03. void  strsort( string  &, string  &);
04. string  s1= "abc" ;
05. string  s2= "baihe" ;
06. strsort(s1,s2);
07. cout&lt;&lt;s1&lt;&lt;endl;
08. cout&lt;&lt;s2&lt;&lt;endl;
09. return  0;
10. }
11. void  strsort( string  & s1, string  &s2)
12. {
13. string  p;
14. p=s1;
15. s1=s2;
16. s2=p;
17. }

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

五:指针引用:

01. int  main()
02. {
03. void  charsort2( char \*& , char \*&);
04. char  * s1= "abc" ;
05. char  * s2= "baihe" ;
06. charsort2 (s1,s2);
07. cout&lt;&lt;s1&lt;&lt;endl;
08. cout&lt;&lt;s2&lt;&lt;endl;
09. return  0;
10. }
11. void  charsort2( char  \*&s1, char  \*&s2)
12. {
13. char  \*p;
14. p=s1;
15. s1=s2;
16. s2=p;
17. }