C语言 strlen

C语言 strlen

#include <string.h>
size_t strlen(const char *s);

功能:计算指定指定字符串s的长度,不包含字符串结束符‘’

参数:

  • s:字符串首地址

返回值:字符串s的长度,size_t为unsigned int类型

案例

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

int main(void)
{
    // strlen()
    // 计算字符串有效个数
    char ch5[100] = "hello world";
    printf("数组大小:%d
", sizeof(ch5));
    printf("字符串长度:%d
",strlen(ch5));
    // 代码实现计算字符串有效个数
    int len = 0;
    while (ch5[len] != '
')len++;
    printf("字符串长度:%d
", len);

    return 0;
}
strlen 使用案例
原文地址:https://www.cnblogs.com/xiangsikai/p/12373714.html