要注意使用断言判断传入的字符串非空。

 #include <cassert>

 //求字符串长度
size_t StrLen(const char *str)
{
assert(str != nullptr);
size_t len = ;
while (*str++ != '\0')
{
++len;
}
return len;
} //复制字符串
char* StrCpy(char *dest, const char *src)
{
assert(dest != nullptr && src != nullptr);
char *temp = dest;
while ((*dest++ = *src++) != '\0');
return temp;
} //复制指定长度字符串
char* StrNCpy(char *dest, const char *src, size_t n)
{
assert(dest != nullptr && src != nullptr);
char *temp = dest;
size_t i = ;
while (i++ < n && (*dest++ = *src++) != '\0');
if (*dest != '\0')
{
*dest = '\0';
}
return temp;
} //比较字符串
int StrCmp(const char *lhs, const char *rhs)
{
assert(lhs != nullptr && rhs != nullptr);
int ret = ;
while (!(ret = *(const unsigned char *)lhs - *(const unsigned char *)rhs) && *rhs)
{
++lhs;
++rhs;
}
if (ret > )
{
return ;
}
else if (ret < )
{
return -;
}
else
{
return ;
}
} //拼接字符串
char* StrCat(char *dest, const char *src)
{
assert(dest != nullptr && src != nullptr);
char *temp = dest;
while (*dest != '\0')
{
++dest;
}
while ((*dest++ = *src++) != '\0');
return temp;
} //附:实现memcpy()函数
void* MemCpy(void *dest, const void *src, size_t n)
{
assert(dest != nullptr && src != nullptr);
//不可直接对void类型的指针进行操作
char *temp_dest = (char*)dest;
const char *temp_src = (char*)src;
while (n--)
{
*temp_dest++ = *temp_src++;
}
return dest;
} //附2:实现memset()函数
void* MemSet(void *buffer, int c, size_t n)
{
assert(buffer != nullptr);
char *temp = (char*)buffer;
while (n--)
{
*temp++ = (char)c;
}
return buffer;
}

c++的c风格字符串函数的实现的更多相关文章

  1. c风格字符串函数

    十一.C 风格字符串  1)字符串操作  strcpy(p, p1) 复制字符串  strncpy(p, p1, n) 复制指定长度字符串  strcat(p, p1) 附加字符串  strncat( ...

  2. 标准库string与C风格字符串

    返回字符串的长度 string标准库 #include<iostream> #include<cstring> using namespace std; int main() ...

  3. 第6章 字符串(上)——C风格字符串

    6.1 C-strings(C 风格字符串) C风格字符串: 字符数组是元素为字符型的数组,字符串是以空字符'\0' 作为数组最后一个元素的字符数组. 如果指定了数组的大小,而字符串的长度又小于数组大 ...

  4. C风格字符串与C++风格字符串

    C风格字符串与C++风格字符串 C风格字符串:对字符串进行操作的 C 函数定义在头文件<cstring>中: 1. 字符串定义:char* result: 2. 字符串的最后一个字符是nu ...

  5. PHP函数积累总结(Math函数、字符串函数、数组函数)

    Math函数:10个较常用标红.abs — 绝对值acos — 反余弦acosh — 反双曲余弦asin — 反正弦asinh — 反双曲正弦atan2 — 两个参数的反正切atan — 反正切ata ...

  6. string字符串转C风格字符串 进而转换为数字

    要求如题 头文件stdlib.h中有一个函数atof() 可以将字符串转化为双精度浮点数(double) double atof(const char *nptr); 此字符串为C风格字符串,因此需要 ...

  7. C++ 标准头文件与C头文件区别与联系以及C风格字符串

    1.cstdlib是C++里面的一个常用头文件, 等价于C中的<stdlib.h>. 2.一般一个带“.h” 扩展名的库文件,比如iostream.h.这是延续C语言的,为了兼容C.在新标 ...

  8. C 和 C++ 字符串函数操作

    1)字符串操作  strcpy(p, p1) 复制字符串 strncpy(p, p1, n) 复制指定长度字符串 strcat(p, p1) 附加字符串 strncat(p, p1, n) 附加指定长 ...

  9. 07 --C语言字符串函数

    1)字符串操作  复制 strcpy(p, p1)      复制字符串 strncpy(p, p1, n)  复制指定长度字符串 strdup(char *str)      将串拷贝到新建的位置处 ...

随机推荐

  1. Deep Learning(3)算法简介

    查看最新论文 Yoshua Bengio, Learning Deep Architectures for AI, Foundations and Trends in Machine Learning ...

  2. [golang note] 协程基础

    协程概念 √ 协程通常称为coroutine,在golang中称为goroutine. √ 协程本质上是一种用户态线程,它不需要操作系统来进行抢占式调度,在实际实现中寄存在线程之中. √ 协程系统开销 ...

  3. 121. Best Time to Buy and Sell Stock(股票最大收益)

    Say you have an array for which the ith element is the price of a given stock on day i. If you were ...

  4. Hello 2019 Solution

    A. Gennady and a Card Game 签到. #include <bits/stdc++.h> using namespace std; ], t[]; bool solv ...

  5. 2018 Multi-University Training Contest 7 Solution

    A - Age of Moyu 题意:给出一张图,从1走到n,如果相邻两次走的边的权值不同,花费+1, 否则花费相同,求最小花费 思路:用set记录有当前点的最小花费有多少种方案到达,然后最短路 #i ...

  6. windows监听socket和标准输入

    原来的代码 def input_command(self): msg = raw_input('\nPlease input the command:') remote_id = raw_input( ...

  7. linux 异常

    1. NoRouteToHostException异常问题的原因及解决 (转自:http://performtest163.blog.163.com/blog/static/1400769642011 ...

  8. php面向对象基础知识点总结

    下面是一些PHP面向对象基础知识的摘录,摘录内容来自PHP5.1手册. 1.类的变量成员叫做“属性”,或者叫“字段”.“特征”,在本文档统一称为“属性”. 2.属性中的变量可以初始化,但是初始化的值必 ...

  9. Spring MVC同一方法返回JSON/XML格式

    最近一道面试题,要求同一API接口支持不同格式返回值.一开始是设想通过过滤器(Filter)设置返回值,但是并不可行,因为方法返回值一般都是类型需要做转换,而过滤器则是前置的.另一方面可以通过拦截器的 ...

  10. mongodb的存储引擎

    mongodb版本为3.4 mongodb存储引起的一些概述 存储引擎是MongoDB的核心组件,负责管理数据如何存储在硬盘和内存上.从MongoDB 3.2 版本开始,MongoDB 支持多数据存储 ...