C语言中的字符串函数有如下这些

  • 获取字符串长度

    • strlen
  • 长度不受限制的字符串函数
    • strcpy
    • strcat
    • strcmp
  • 长度受限制的字符串函数
    • strncpy
    • strncat
    • strncmp
  • 字符串查找
    • strstr
    • strtok
  • 错误信息报告
    • strerror

接下来看看如何实现它们

获取字符串长度

strlen

我们看看文档是怎样说的,如下

strlen文档

size_t strlen ( const char * str );

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字符串长度是由'\0'来确定的,也就是说从字符串的第一个开始只要遇到'\0'就结束长度计算(不包含'\0')

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,然后mystr 就已经被初始化为一个长度为11的字符串了。所以呢, sizeof(mystr) 会得出 100, 而strlen(mystr) 会返回 11.

综上,可以知道

  1. 字符串已经 '\0' 作为结束标志,strlen函数返回的是在字符串中 '\0' 前面出现的字符个数(不包含 '\0' )。
  2. 该函数只认'\0',参数指向的字符串必须要以 '\0' 结束。
  3. 注意函数的返回值为size_t,是无符号的

实现

strlen函数的实现有好几种。

比如

  1. 计数器的方法
  2. 递归
  3. 指针 - 指针

接下来一一实现。

1. 计数器:使用一个变量来记录 - count

断言指针不为空是个好习惯~

int my_strlen(char* str)
{
int count = 0;
assert(str != NULL);
while (*str != '\0') // while (*str)
{
count++;
str++;
}
return count;
}

就一直找'\0',当*str不是'\0'时,就count++,str++,直到遇到'\0'停止,然后返回count就是长度了。

2. 递归

断言指针不为空是个好习惯~

int my_strlen(char* str)
{
assert(str != NULL);
char* p = str;
while(*p == '\0')
{
return 0;
}
return 1 + my_strlen(p + 1);
}

比如传入的str地址为 1000

那么 1 + my_strlen(p + 1) 中,p + 1,指针偏移后就是1001,以此类推。

1 + 1 + my_strlen(p + 1)

1 + 1 + 1 + my_strlen(p + 1)

1 + 1 + 1 + 1 + my_strlen(p + 1)

...

1 + 1 + 1 + 1 + ... + 0

最终就可以得出长度。

3. 指针-指针

断言指针不为空是个好习惯~

int my_strlen(char* str)
{
assert(str != NULL);
char* p = str;
while (*p != '\0')
{
p++;
}
return p - str;
}

把指针str的地址赋值给一个新的指针p,str作为指向起始地址的指针,不改变它,记录起始地址。

然后通过指针p进行查找'\0',判断当前字符是否为'\0',不是就进行p++,然后继续判断下一个字符,如此循环,直到指针p找到'\0',然后用 当前的指针p 减去 起始指针str 进行返回,就是长度了。

C语言-字符串函数的实现(一)之strlen的更多相关文章

  1. 13-C语言字符串函数库

    目录: 一.C语言字符串函数库 二.用命令行输入参数 回到顶部 一.C语言字符串函数库 1 #include <string.h> 2 字符串复制 strcpy(参数1,参数2); 参数1 ...

  2. C语言字符串函数大全

    C语言字符串函数大全 函数名: stpcpy 功 能: 拷贝一个字符串到另一个 用 法: char *stpcpy(char *destin, char *source); 程序例: #include ...

  3. C语言-字符串函数的实现(五)之strstr

    C语言中的字符串函数有如下这些 获取字符串长度 strlen 长度不受限制的字符串函数 strcpy strcat strcmp 长度受限制的字符串函数 strncpy strncat strncmp ...

  4. C语言-字符串函数的实现(二)之strcpy

    C语言中的字符串函数有如下这些 获取字符串长度 strlen 长度不受限制的字符串函数 strcpy strcat strcmp 长度受限制的字符串函数 strncpy strncat strncmp ...

  5. C语言字符串函数例子程序大全 – string相关

    关于字符串函数的应用细则,例子程序 – jerny 函数名: stpcpy 功 能: 拷贝一个字符串到另一个 用 法: char *stpcpy(char *destin, char *source) ...

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

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

  7. 关于C语言字符串函数使用的一点心得

    就字符串的拼接函数为例strcat. 原型:extern char *strcat(char *dest,char *src);用法:#include <string.h> 功能:把src ...

  8. C语言字符串函数

    strtok()     字符串分割函数strstr()     字符串查找函数 范例 #include <string.h> main() {     char * s = " ...

  9. c语言字符串函数大全(转)

    函数名: stpcpy 功 能: 拷贝一个字符串到另一个 用 法: char *stpcpy(char *destin, char *source); 程序例: #include <stdio. ...

随机推荐

  1. Baccarat是如何运用去中心化治理模式的?

    区块链的出现,让大家看到了去中心化的可能.去中心化的数字资产从最初的默默无闻,一路起起伏伏发展了十年,逐渐成为了大众认可的价值存储方式.去中心化的金融,使数字资产的生态建设者意识到,即使没有中心化的金 ...

  2. 详解支付体系颠覆者NGK公链:如何通过呼叫河马智能合约加速转账?

    纵观全球加密货币市场,至今为止,全球已经发行的加密货币以及数字代币的数量已经超过了7000种,且未来还将会有更多的加密货币或数字代币出现.在众多加密货币项目中,投资者很难在众多的项目里甄别项目的好坏以 ...

  3. SharedPreferences 数据传输中遇到的一些问题总结

    原构想:MainActivity 设置两个按钮,btn1--跳转Main2Activity通过复选框组选择并提交,btn2--跳转Main3Activity通过RecycleView显示选择结果. 主 ...

  4. Python算法_斐波那契数列(10)

    写一个函数,输入 n ,求斐波那契(Fibonacci)数列的第 n 项.斐波那契数列的定义如下: F(0) = 0,   F(1) = 1F(N) = F(N - 1) + F(N - 2), 其中 ...

  5. linux系统解压命令总结

    原文链接:https://www.cnblogs.com/lhm166/articles/6604852.html tar -c: 建立压缩档案 -x:解压 -t:查看内容 -r:向压缩归档文件末尾追 ...

  6. C语言:贪心算法之装箱问题

    #include <stdio.h> #include <stdlib.h> #define N 6 #define V 100 typedef struct box // 使 ...

  7. 后端程序员之路 15、Matplotlib

    Matplotlib: Python plotting - Matplotlib 2.0.0 documentationhttp://matplotlib.org/ matplotlib-绘制精美的图 ...

  8. C# 中 string.Empty、""、null的差别

    一.string.Empty 和 ""                                                           原文1   原文2 1. ...

  9. Linux 虚拟文件系统四大对象:超级块、inode、dentry、file之间关系

    更多嵌入式原创文章,请关注公众号:一口Linux 一:文件系统 1. 什么是文件系统? 操作系统中负责管理和存储文件信息的软件机构称为文件管理系统,简称文件系统. 通常文件系统是用于存储和组织文件的一 ...

  10. 解决.NET Core Ajax请求后台传送参数过大请求失败问题

    解决.NET Core Ajax请求后台传送参数过大请求失败问题 今天在项目上遇到一个坑, 在.Net Core中通过ajax向mvc的controller传递对象时,控制器(controller)的 ...