ITeye博客

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

dhj · 2008-10-10 10:39 · c&c++ · Markdown

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

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

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

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。