C++每日练笔之字符串连接函数

 1 //===============================写在前面===============================
 2 在标准C++中,有两种形式的字符:
 3 分别是C语言的C风格字符串和C++标准库中的string类。
 4 
 5 其中C风格的字符串以字符数组 char*或者char[]来实现,
 6 但是功能很弱,而且很不安全。
 7 
 8 标准C++采用类模板的形式来封装字符串,建议使用。
 9 
10 头文件说明:
11 
12 C风格字符串: 
13 #include <string.h> //这是C语言和非标准C++用的老式的头文件
14 #include <cstring>  //这是标准C++用的新式的头文件
15 //string.h和cstring在功能上基本类似
16 //只是后者的内容都放在命名空间std中
17 
18 标准C++的string类头文件
19 #include <string> //所有内容放在命名空间std中
20 
21 注1:
22     string.h和cstring基本相同,后者是前者的替代版本。
23     但是和string完全是两个不同的头文件。
24 
25 注2:在MFC中还有一个功能更强大的类CString类来处理字符串
26 //==========================================================================
27 对于C风格的字符串,操作很不安全,
28 包括C++提供的两个字符串连接函数strcat和strncat。
29 因此我写了多个函数用于字符串连接,并放在命名空间TYCppStdLib中。
30 
31 其中我写的strcat和同样是不安全的。
32 但相比C++提供的strcat要好一点,避免了因为空指针而程序崩溃的情况。
33 另外我写的五个重载函数strcatEx利用安全的string为中间媒介,安全可靠。
34 //==========================================================================

 

 1 /*- ==========================================================
 2 *     文件名  :TYStringAndCharFunc.h
 3 *     开发人员:袁培荣
 4 *     当前版本:1.0.0.2595
 5 *     创建时间:2012-04-23
 6 *     修改时间:2012-04-30
 7 *     功能说明:字符串处理函数
 8 *     版权说明:版权所有 袁培荣 YuanPeirong 
 9 *     编译环境:Windows 7(x64) SP1 简体中文专业版
10 *     编译器:  Visual Studio 2010 SP1(中文旗舰版)
11                 MinGW 2011118
12                 Visual C++ 6.0 SP6(中文企业版)
13 - ==========================================================*/
14 
15 #ifndef TYStringAndCharFunc_H_TYCppStdLib
16 #define TYStringAndCharFunc_H_TYCppStdLib
17 
18 #ifdef TYStringAndCharFunc_DLL_API
19 #else
20 #define TYStringAndCharFunc_DLL_API _declspec(dllimport)
21 #endif
22 
23 #include <string>
24 
25 using namespace std;
26 
27 namespace TYCppStdLib
28 {   
29     //String
30     
31     //Char
32         //获取字符的ACSII码
33     TYStringAndCharFunc_DLL_API int Asc(char ch);
34     TYStringAndCharFunc_DLL_API int Asc(char *ch);
35     
36     
37     
38     //StringAndChar
39         //字符串连接
40     TYStringAndCharFunc_DLL_API void strcat(
41         char *s,          //C风格字符串,连接结果存于s中
42         char *d           //C风格字符串
43     ); //与std中的strcat同名,调用时加TYCppStdLib:: 调试模式出错
44     
45     TYStringAndCharFunc_DLL_API string strcatEx(
46         const char *c1,   //C风格字符串
47         const char *c2    //C风格字符串
48     ); //以string类型返回c1和c2连接后的字符串
49     
50     TYStringAndCharFunc_DLL_API string strcatEx(
51         const char *c1,   //C风格字符串
52         const string &s2  //C++string字符串
53     ); //以string类型返回c1和s2连接后的字符串
54     
55     TYStringAndCharFunc_DLL_API string strcatEx(
56         const string &s1, //C++string字符串
57         const char *c2    //C风格字符串
58     ); //以string类型返回s1和c2连接后的字符串
59     
60     TYStringAndCharFunc_DLL_API void strcatEx(
61         const char *c1,   //C风格字符串
62         string &s2        //C++string字符串
63     ); //将c1和s2连接后的字符串存于s2中
64     
65     TYStringAndCharFunc_DLL_API void strcatEx(
66         string &s1,       //C++string字符串
67         const char *c2    //C风格字符串
68     ); //将s1和c2连接后的字符串存于s1中
69 
70 }
71 
72 #endif
 1 /*- ==========================================================
 2 *     文件名  :TYStringAndCharFunc.cpp
 3 *     开发人员:袁培荣
 4 *     当前版本:1.0.0.2595
 5 *     创建时间:2012-04-23
 6 *     修改时间:2012-04-30
 7 *     功能说明:字符串处理函数
 8 *     版权说明:版权所有 袁培荣 YuanPeirong 
 9 *     编译环境:Windows 7(x64) SP1 简体中文专业版
10 *     编译器:  Visual Studio 2010 SP1(中文旗舰版)
11                 MinGW 2011118
12                 Visual C++ 6.0 SP6(中文企业版)
13 - ==========================================================*/
14 
15 #ifndef TYStringAndCharFunc_DLL_ForAPI
16 #define TYStringAndCharFunc_DLL_ForAPI
17 
18 #define TYStringAndCharFunc_DLL_API _declspec(dllexport)
19 
20 #endif
21 
22 #include "http://www.cnblogs.com/Include/StringAndChar/TYStringAndCharFunc.h"
23 #include <cstring>
24 
25 using namespace std;
26 
27 void TYCppStdLib::strcat(char *s, char *d)
28 {
29     int i, ls, ld;
30     if(!s || !d)
31         return;
32     ls=strlen(s);
33     ld=strlen(d);
34     for(i=0;i<=ld;i++)
35         s[ls+i]=d[i]; 
36 }
37 
38 string TYCppStdLib::strcatEx(const char *c1, const char *c2)
39 {
40     string s1="",s2="";
41     if(c1)
42         s1=c1;
43     if(c2)
44         s2=c2;
45     return (s1+s2);
46 }
47 
48 string TYCppStdLib::strcatEx(const char *c1, const string &s2)
49 {
50     string s1="";
51     if(c1)
52         s1=c1;
53     return (s1+s2);
54 }
55 
56 string TYCppStdLib::strcatEx(const string &s1, const char *c2)
57 {
58     string s2="";
59     if(c2)
60         s2=c2;
61     return (s1+s2);
62 }
63 
64 void TYCppStdLib::strcatEx(const char *c1, string &s2)
65 {
66     string s1="";
67     if(c1)
68         s1=c1;
69     s2=s1+s2;
70 }
71 
72 void TYCppStdLib::strcatEx(string &s1, const char *c2)
73 {
74     string s2="";
75     if(c2)
76         s2=c2;
77     s1=s1+s2;
78 }

 

posted @ 2012-05-01 00:41  天远  阅读(2420)  评论(0)    收藏  举报

版权所有 © 2010-2020 YuanPeirong TianYuan All Rights Reserved. Powered By 天远