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

//===============================写在前面===============================
在标准C++中,有两种形式的字符:
分别是C语言的C风格字符串和C++标准库中的string类。

其中C风格的字符串以字符数组 char*或者char[]来实现,
但是功能很弱,而且很不安全。

标准C++采用类模板的形式来封装字符串,建议使用。

头文件说明:

C风格字符串: 
#include <string.h> //这是C语言和非标准C++用的老式的头文件
#include <cstring>  //这是标准C++用的新式的头文件
//string.h和cstring在功能上基本类似
//只是后者的内容都放在命名空间std中

标准C++的string类头文件
#include <string> //所有内容放在命名空间std中

注1:
    string.h和cstring基本相同,后者是前者的替代版本。
    但是和string完全是两个不同的头文件。

注2:在MFC中还有一个功能更强大的类CString类来处理字符串
//==========================================================================
对于C风格的字符串,操作很不安全,
包括C++提供的两个字符串连接函数strcat和strncat。
因此我写了多个函数用于字符串连接,并放在命名空间TYCppStdLib中。

其中我写的strcat和同样是不安全的。
但相比C++提供的strcat要好一点,避免了因为空指针而程序崩溃的情况。
另外我写的五个重载函数strcatEx利用安全的string为中间媒介,安全可靠。
//==========================================================================

/*- ==========================================================
*     文件名  :TYStringAndCharFunc.h
*     开发人员:袁培荣
*     当前版本:1.0.0.2595
*     创建时间:2012-04-23
*     修改时间:2012-04-30
*     功能说明:字符串处理函数
*     版权说明:版权所有 袁培荣 YuanPeirong 
*     编译环境:Windows 7(x64) SP1 简体中文专业版
*     编译器:  Visual Studio 2010 SP1(中文旗舰版)
                MinGW 2011118
                Visual C++ 6.0 SP6(中文企业版)
- ==========================================================*/

#ifndef TYStringAndCharFunc_H_TYCppStdLib
#define TYStringAndCharFunc_H_TYCppStdLib

#ifdef TYStringAndCharFunc_DLL_API
#else
#define TYStringAndCharFunc_DLL_API _declspec(dllimport)
#endif

#include <string>

using namespace std;

namespace TYCppStdLib
{   
    //String
    
    //Char
        //获取字符的ACSII码
    TYStringAndCharFunc_DLL_API int Asc(char ch);
    TYStringAndCharFunc_DLL_API int Asc(char *ch);
    
    
    
    //StringAndChar
        //字符串连接
    TYStringAndCharFunc_DLL_API void strcat(
        char *s,          //C风格字符串,连接结果存于s中
        char *d           //C风格字符串
    ); //与std中的strcat同名,调用时加TYCppStdLib:: 调试模式出错
    
    TYStringAndCharFunc_DLL_API string strcatEx(
        const char *c1,   //C风格字符串
        const char *c2    //C风格字符串
    ); //以string类型返回c1和c2连接后的字符串
    
    TYStringAndCharFunc_DLL_API string strcatEx(
        const char *c1,   //C风格字符串
        const string &s2  //C++string字符串
    ); //以string类型返回c1和s2连接后的字符串
    
    TYStringAndCharFunc_DLL_API string strcatEx(
        const string &s1, //C++string字符串
        const char *c2    //C风格字符串
    ); //以string类型返回s1和c2连接后的字符串
    
    TYStringAndCharFunc_DLL_API void strcatEx(
        const char *c1,   //C风格字符串
        string &s2        //C++string字符串
    ); //将c1和s2连接后的字符串存于s2中
    
    TYStringAndCharFunc_DLL_API void strcatEx(
        string &s1,       //C++string字符串
        const char *c2    //C风格字符串
    ); //将s1和c2连接后的字符串存于s1中

}

#endif

/*- ==========================================================
*     文件名  :TYStringAndCharFunc.cpp
*     开发人员:袁培荣
*     当前版本:1.0.0.2595
*     创建时间:2012-04-23
*     修改时间:2012-04-30
*     功能说明:字符串处理函数
*     版权说明:版权所有 袁培荣 YuanPeirong 
*     编译环境:Windows 7(x64) SP1 简体中文专业版
*     编译器:  Visual Studio 2010 SP1(中文旗舰版)
                MinGW 2011118
                Visual C++ 6.0 SP6(中文企业版)
- ==========================================================*/

#ifndef TYStringAndCharFunc_DLL_ForAPI
#define TYStringAndCharFunc_DLL_ForAPI

#define TYStringAndCharFunc_DLL_API _declspec(dllexport)

#endif

#include "../../Include/StringAndChar/TYStringAndCharFunc.h"
#include <cstring>

using namespace std;

void TYCppStdLib::strcat(char *s, char *d)
{
    int i, ls, ld;
    if(!s || !d)
        return;
    ls=strlen(s);
    ld=strlen(d);
    for(i=0;i<=ld;i++)
        s[ls+i]=d[i]; 
}

string TYCppStdLib::strcatEx(const char *c1, const char *c2)
{
    string s1="",s2="";
    if(c1)
        s1=c1;
    if(c2)
        s2=c2;
    return (s1+s2);
}

string TYCppStdLib::strcatEx(const char *c1, const string &s2)
{
    string s1="";
    if(c1)
        s1=c1;
    return (s1+s2);
}

string TYCppStdLib::strcatEx(const string &s1, const char *c2)
{
    string s2="";
    if(c2)
        s2=c2;
    return (s1+s2);
}

void TYCppStdLib::strcatEx(const char *c1, string &s2)
{
    string s1="";
    if(c1)
        s1=c1;
    s2=s1+s2;
}

void TYCppStdLib::strcatEx(string &s1, const char *c2)
{
    string s2="";
    if(c2)
        s2=c2;
    s1=s1+s2;
}

转载于:https://my.oschina.net/u/186539/blog/55699

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值