C语言 strlen

C语言 strlen

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

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

参数:

  • 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\n", sizeof(ch5));
    printf("字符串长度:%d\n",strlen(ch5));
    // 代码实现计算字符串有效个数
    int len = 0;
    while (ch5[len] != '\n')len++;
    printf("字符串长度:%d\n", len);

    return 0;
}
strlen 使用案例

 

posted @ 2020-02-27 19:01  kevin.Xiang  阅读(555)  评论(0编辑  收藏  举报