c语言strlen函数_C和C ++教程中的strlen()函数以及示例

c语言strlen函数

c语言strlen函数

C and C++ programming languages provide the strlen() function in order to calculate or return the size of the given string or char array. String and char array is the same data type in C and C++ which simply a character array. strlen() function provided from the string.h header or library.

C和C ++编程语言提供strlen()函数,以便计算或返回给定字符串或char数组的大小。 字符串和字符数组与C和C ++中的数据类型相同,只是字符数组。 从string.h标头或库提供的strlen()函数。

strlen()函数语法 (strlen() Function Syntax)

strlen() function has the following syntax where we provide the char array or string as a parameter.

strlen()函数具有以下语法,其中我们将char数组或字符串作为参数提供。

size_t strlen(const char *STR)
  • size_t is the return value type of the strlen() function which is generally integer or long which returns the size or length of the given string or char array.

    size_t是strlen()函数的返回值类型,通常为整数或long,它返回给定字符串或char数组的大小或长度。

  • const char *STR specifies the string or char array we want to get length or size.

    const char *STR指定要获取长度或大小的字符串或char数组。

字符串的长度 (String Length Of A String)

We will start by calculating the length of the given string. Actually we will provide a char array where it will be presented in string format. We will provide the string named a and b with values My name is poftut.com and I love poftut.com. We will calculate these strings or sentences length with the strlen() function.

我们将从计算给定字符串的长度开始。 实际上,我们将提供一个char数组,该数组将以字符串格式显示。 我们将为字符串a和b提供值, My name is poftut.comI love poftut.com 。 我们将使用strlen()函数计算这些字符串或句子的长度。

#include <stdio.h>
#include <string.h>

int main()
{
    char a[100]="My name is poftut.com";

    char b[100]="I love poftut.com";
 
    a_length = strlen(a);

    b_length = strlen(b);
    
    printf("Length of string a = %ld \n",a_length);
    
    printf("Length of string b = %ld \n",b_length);
    
    return 0;
}

带字符指针的字符串的字符串长度 (String Length Of A String with Char Pointer)

Alternatively, we can create the string by using char pointer which will actually a char array or string too. We will provide the string named a and b with values My name is poftut.com and I love poftut.com. We will calculate these strings or sentences length with the strlen() function.

另外,我们可以使用char指针创建字符串,而char指针实际上也将是char数组或字符串。 我们将为字符串a和b提供值, My name is poftut.comI love poftut.com 。 我们将使用strlen()函数计算这些字符串或句子的长度。

#include <stdio.h>
#include <string.h>

int main()
{
    char a[100]="My name is poftut.com";

    char b[100]="I love poftut.com";

    printf("Length of string a = %ld \n",strlen(a));

    printf("Length of string b = %ld \n",strlen(b));

    return 0;
}

字符数组的字符串长度 (String Length Of A Char Array)

Alternatively, we can create or define a char array item by item and then calculate the size of the complete char array. We will set the Hello poftut.com sentence characters character by character into char array a.

另外,我们可以逐项创建或定义一个char数组,然后计算整个char数组的大小。 我们将Hello poftut.com字符地将Hello poftut.com句子字符设置为char数组a。

#include <stdio.h>
#include <string.h>

int main()
{
    char a[100]={'H','e','l','l','o',' ','p','o','f','t','u','t','.','c','o','m'};

    printf("Length of string a = %ld \n",strlen(a));

    return 0;
}

输入字符串或字符数组的长度 (Length Of A Input String or Char Array)

We can also get the char array or string from the input by using scanf(). We will read the input from the standard input or console and set into a char array then we will calculate the length with the strlen() function.

我们还可以使用scanf()从输入中获取char数组或字符串。 我们将从标准输入或控制台读取输入,并将其设置为char数组,然后使用strlen()函数计算长度。

#include <stdio.h>
#include <string.h>

int main()
{
    char name[100];
    char sentence[100];


    printf("Please enter your name\n");
    scanf("%s",name);
    printf("Please enter a sentence\n");
    scanf("%s",sentence);
    printf("\n");


    printf("Length of name = %ld \n",strlen(name));
    printf("Length of sentence = %ld \n",strlen(sentence));

    return 0;
}
Length Of A Input String or Char Array
Length Of A Input String or Char Array
输入字符串或字符数组的长度
LEARN MORE  How To Find Specified Strings In Files With Find Command In Windows From Command Line With Examples?
了解更多信息如何在Windows中使用示例从命令行在Windows中使用Find命令在文件中查找指定的字符串?

翻译自: https://www.poftut.com/strlen-function-in-c-and-c-tutorial-with-examples/

c语言strlen函数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值