交换两个字符串数据。
一:字符指针
交换两个字符串数据。
一:字符指针
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. }
二:字符数组
三:字符串
- int main()
- {
- void strsort( string *, string *);
- string s1= "abc" ;
- string s2= "baihe" ;
- strsort(&s1,&s2);
- cout<<s1<<endl;
- cout<<s2<<endl;
- return 0;
- }
- void strsort( string *s1, string *s2)
- {
- string p;
- p=*s1;
- *s1=*s2;
- *s2=p;
- }
四:引用
- int main()
- {
- void strsort( string &, string &);
- string s1= "abc" ;
- string s2= "baihe" ;
- strsort(s1,s2);
- cout<<s1<<endl;
- cout<<s2<<endl;
- return 0;
- }
- void strsort( string & s1, string &s2)
- {
- string p;
- p=s1;
- s1=s2;
- s2=p;
- }
多谢论坛上朋友的分享.以下为新添方法:
五:指针引用:
- int main()
- {
- void charsort2( char *& , char *&);
- char * s1= "abc" ;
- char * s2= "baihe" ;
- charsort2 (s1,s2);
- cout<<s1<<endl;
- cout<<s2<<endl;
- return 0;
- }
- void charsort2( char *&s1, char *&s2)
- {
- char *p;
- p=s1;
- s1=s2;
- s2=p;
- }