C++ - C - strlen

1. C++ - std::strlen

size_t strlen ( const char * str );

定义于头文件 <cstring>

Get string length - 获取字符串长度

Returns the length of the C string str.
返回 C 字符串 str 的长度。

The length of a C string is determined by the terminating null-character: A C string is as long as the number of characters between the beginning of the string and the terminating null character (without including the terminating null character itself).
C 字符串的长度由终止的空字符确定:C 字符串的长度与字符串开头和终止的空字符之间的字符数一样长 (不包括终止的空字符本身)。

返回给定字节字符串的长度,即首元素为 str 所指向的字符数组直到而不包含首个空字符的字符数。若 str 所指向的字符数组中无空字符,则行为未定义。

This should not be confused with the size of the array that holds the string. For example:
这不应与保存字符串的数组的大小混淆。例如:

char mystr[100]="test string";

defines an array of characters with a size of 100 chars, but the C string with which mystr has been initialized has a length of only 11 characters. Therefore, while sizeof(mystr) evaluates to 100, strlen(mystr) returns 11.
定义了一个字符数组,其大小为 100 个字符,但是用于初始化 mystr 的 C 字符串的长度仅为 11 个字符。因此,虽然 sizeof(mystr) 的值为 100,strlen(mystr) 返回 11。

In C++, char_traits::length implements the same behavior.
在 C++ 中,char_traits::length 实现相同的行为。

1.1 Parameters

str - C string.
C 字符串。指向要检验的空终止字节字符串的指针。

1.2 Return Value

The length of string.
字符串的长度。空终止字符串 str 的长度。

1.3 Possible Implementation

//============================================================================
// Name        : std::strlen
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================std::size_t strlen(const char *start)
{const char *end = start;while (*end++ != 0){;}return end - start - 1;
}

*cp++
后缀 ++ 操作符的优先级高于 * 操作符。该表达式可以分解为三个步骤:

  1. ++ 操作符产生 cp 的一份拷贝;
  2. ++ 操作符增加 cp 的值;
  3. 在 cp 的拷贝上执行间接访问操作。

2. C++ - Example

2.1 C - strlen

//============================================================================
// Name        : strlen
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================#include <stdio.h>
#include <string.h>int main()
{char szInput[256];printf("Enter a sentence: ");gets(szInput);printf("The sentence entered is %u characters long.\n", (unsigned) strlen(szInput));return 0;
}
Enter a sentence: yong
The sentence entered is 4 characters long.Enter a sentence: qiang
The sentence entered is 5 characters long.Enter a sentence: yong qiang
The sentence entered is 10 characters long.

2.2 C++ - std::strlen

//============================================================================
// Name        : std::strlen
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================#include <cstring>
#include <iostream>int main()
{const char str[] = "yong qiang";std::cout << "without null character: " << std::strlen(str) << '\n'<< "with null character: " << sizeof str << '\n';return 0;
}
without null character: 10
with null character: 11

3. strlen - strnlen_s

size_t strlen( const char *str );
size_t strnlen_s( const char *str, size_t strsz );

Defined in header <string.h>

Returns the length of the given null-terminated byte string, that is, the number of characters in a character array whose first element is pointed to by str up to and not including the first null character.
返回给定空终止字符串的长度,即首元素为 str 所指,且不包含首个空字符的字符数组中的字符数。

The behavior is undefined if str is not a pointer to a null-terminated byte string.
str 不是指向空终止字节字符串的指针则行为未定义。

Same as (1), except that the function returns zero if str is a null pointer and returns strsz if the null character was not found in the first strsz bytes of str.
同 (1),除了若 str 为空指针则返回零,而若在 str 的首 strsz 个字节找不到空字符则返回 strsz

The behavior is undefined if both str points to a character array which lacks the null character and the size of that character array < strsz; in other words, an erroneous value of strsz does not expose the impending buffer overflow.
str 指向缺少空字符的字符数组且该字符数组的大小 < strsz 则行为未定义,换言之,strsz 的错误值不会暴露行将来临的缓冲区溢出。

As with all bounds-checked functions, strnlen_s is only guaranteed to be available if __STDC_LIB_EXT1__ is defined by the implementation and if the user defines __STDC_WANT_LIB_EXT1__ to the integer constant 1 before including string.h.
同所有边界检查函数,strnlen_s 仅若实现定义了 __STDC_LIB_EXT1__ ,且用户在包含 <string.h> 前定义 __STDC_WANT_LIB_EXT1__ 为整数常量 1 才保证可用。

3.1 Parameters

str - pointer to the null-terminated byte string to be examined (指向要检测的空终止字符串的指针)
strsz - maximum number of characters to examine (要检测的最大字符数量)

3.2 Return value

  • The length of the null-terminated byte string str. (空终止字节字符串 str 的长度。)
  • The length of the null-terminated byte string str on success, zero if str is a null pointer, strsz if the null character was not found. (成功时为空终止字节字符串 str 的长度,若 str 是空指针则为零,若找不到空字符则为 strsz。)

strnlen_s and wcsnlen_s are the only bounds-checked functions that do not invoke the runtime constraints handler. They are pure utility functions used to provide limited support for non-null terminated strings.
strnlen_swcsnlen_s 是仅有的不调用运行时制约处理的边界检查函数。它们是用于提供空终止字符串受限制支持的纯功能函数。

4. C - Example

4.1 strlen - strnlen_s

//============================================================================
// Name        : strlen
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================#define __STDC_WANT_LIB_EXT1__ 1
#include <string.h>
#include <stdio.h>int main(void)
{const char str[] = "yong qiang";printf("without null character: %zu\n", strlen(str));printf("with null character:    %zu\n", sizeof str);#ifdef __STDC_LIB_EXT1__printf("without null character: %zu\n", strnlen_s(str, sizeof str));
#endifreturn 0;
}
without null character: 10
with null character:    11

5. 代码实现 strlen

int yong_strlen(const char *str)
{if (NULL == str){return 0;}int len = 0;while (str[len] != '\0'){++len;}return len;
}
int qiang_strlen(const char *str)
{if (NULL == str){return 0;}int len = 0;while (*str){++len;++str;}return len;
}

5.1 Example

//============================================================================
// Name        : std::strlen
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================#include <cstring>
#include <iostream>int yong_strlen(const char *str)
{if (NULL == str){return 0;}int len = 0;while (str[len] != '\0'){++len;}return len;
}int qiang_strlen(const char *str)
{if (NULL == str){return 0;}int len = 0;while (*str){++len;++str;}return len;
}int main()
{const char str[] = "yong qiang";std::cout << "without null character: " << std::strlen(str) << '\n'<< "with null character: " << sizeof str << '\n';std::cout << "without null character: " << yong_strlen(str) << '\n'<< "with null character: " << sizeof str << '\n';std::cout << "without null character: " << qiang_strlen(str) << '\n'<< "with null character: " << sizeof str << '\n';const char str_data[] = "";std::cout << "without null character: " << std::strlen(str_data) << '\n'<< "with null character: " << sizeof str_data << '\n';std::cout << "without null character: " << yong_strlen(str_data) << '\n'<< "with null character: " << sizeof str_data << '\n';std::cout << "without null character: " << qiang_strlen(str_data) << '\n'<< "with null character: " << sizeof str_data << '\n';return 0;
}
without null character: 10
with null character: 11
without null character: 10
with null character: 11
without null character: 10
with null character: 11
without null character: 0
with null character: 1
without null character: 0
with null character: 1
without null character: 0
with null character: 1
请按任意键继续. . .

References

http://www.cplusplus.com/reference/cstring/strlen/
https://en.cppreference.com/w/c/string/byte/strlen
https://en.cppreference.com/w/cpp/string/byte/strlen

C++ - C - strlen相关推荐

  1. 解决Visual Studio禁止使用strlen函数的问题

    问题描述: 在学习C++的复制构造函数以及复制赋值运算符的重载时,需要用到使用C风格的字符串作为引入,由于我用的是VS2015(社区版),在编译时出错.编译器提醒strcpy函数是不安全的,建议改用s ...

  2. C语言的sizeof和strlen

    strlen是函数,而sizeof是算符.strlen需要进行一次函数调用,而对于sizeof而言,因为缓冲区已经用已知字符串进行了初始化,起长度是固定的,所以sizeof在编译时计算缓冲区的长度. ...

  3. 模拟实现: strstr strcpy strlen strcat strcmp memcpy memmove

    模拟实现: strstr strcpy strlen strcat strcmp memcpy memmove ================================ 1 strstr 字符 ...

  4. 转:strcat与strcpy与strcmp与strlen

    转自:http://blog.chinaunix.net/uid-24194439-id-90782.html strcat 原型:extern char *strcat(char *dest,cha ...

  5. PHP函数学习nl2br(),strlen(),mb_strlen()

    2019独角兽企业重金招聘Python工程师标准>>> 1 nl2br($str): 注意:n之后的是字母L的小写,不要当做数字1. 函数作用:在$str中的每个新行(\n)之前插入 ...

  6. (C++)strlen(),strcmp(),strcpy(),strcat()用法

    string.h中包含了许多用于字符数组的函数.使用前需要在程序开头加string.h©或cstring(C++)头文件 strlen() 作用:得到字符数组第一个结束符\0前的字符的个数 #incl ...

  7. 【No.1_sizeof与strlen】

    ==[注意]== 程序语言只是我们与计算机交流并让计算机实现我们创造性思想的工具,可以并鼓励深入掌握一门语言,但千万别沉迷于钻某种语言的牛角尖,一定要把握好二者间的度 本帖属不定时连载贴,以试卷的形式 ...

  8. 比较分析与数组相关的sizeof和strlen

    // 形如: int a[]={1,2,3,4,5}; char name[]="abcdef"; 无论是整型数组还是字符数组,数组名作为右值的时候都代表数组首元素的首地址. 数组 ...

  9. 【C语言】模拟实现库函数 strcpy(复制字符串内容) 与 strlen(求字符串长度)

    前言:对于库函数的使用,我们不仅要会使用方法,更要知晓使用原理,而知晓使用原理最好的方法就是模拟相应库函数的使用. 今天我们就来模拟 strcpy(复制字符串内容) 与 strlen(求字符串长度)这 ...

  10. C/C++中strlen(),strcpy(),strcat()以及strcmp()的代码实现--学习笔记

    以下代码是自己学习过程中通过借鉴加上自己的理解编写出的代码已经在VC++2008版本上调试通过,主函数因为很简单所以没有附上. 1. strlen() int my_strlen(char *str) ...

最新文章

  1. SQL-语句实现九九乘法表
  2. jdbc配置文件连接mysql_java jdbc使用配置文件连接数据库:
  3. c语言中有bool型变量吗?
  4. JavaScript实现dijkstra迪杰斯特拉算法(附完整源码)
  5. EasyRTSPClient:基于live555封装的支持重连的RTSP客户端RTSPClient
  6. 解决安装pytorch慢的方法(pip安装)
  7. 超详细Ubuntu Linux安装配置 Tomcat
  8. 腾讯视频app下载2019_腾讯视频主设备如何设置
  9. vue 自定义marquee无缝滚动组件
  10. 求最大公约数(辗转相除)
  11. a+aa+...+aaa..aa表达式输出
  12. @hotmail.com 账户添加别名,重命名到@outlook.com 一系列问题,顺道附上个人解决方法
  13. 官网下载Java连接MySql驱动jar包
  14. python爬虫学习教程,短短25行代码批量下载豆瓣妹子图片
  15. 关于最小二乘法快速计算公式汇总
  16. 计算机环境变量怎么恢复默认,环境变量怎么还原
  17. 一份让你效率翻倍的年终总结
  18. 智能学习 | MATLAB实现基于HS和谐搜索的时间序列未来多步预测
  19. 联想小新Pro 16频繁蓝屏解决方案
  20. 使用豆瓣镜像下载软件

热门文章

  1. java 解析rmc_GPS 0183协议GGA、GLL、GSA、GSV、RMC、VTG解释 + 数据解析 | 技术部落
  2. python程序退出后保存变量_Python将变量存储在一个列表中,每次程序重新启动时都会保存这些变量...
  3. 硬件描述语言(HDL)基础——运算符
  4. AnyShare Family 7 开放架构总览
  5. 作者:周傲英(1965-),男,华东师范大学副校长,长江学者特聘教授,数据科学与工程研究院院长...
  6. 7、江科大stm32视频学习笔记——中断的应用:对射式红外传感器计次旋转编码器计次
  7. 致远项目管理SPM系统之质量检查
  8. Restful 学习笔记1(包含centos boost库安装)
  9. Linux的ctrl-alt-f1...ctrl- alt-f7
  10. 数据库顶会VLDB论文解读:阿里巴巴数据库智能参数优化的创新与实践