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.com
, I 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.com
, I 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;
}
翻译自: https://www.poftut.com/strlen-function-in-c-and-c-tutorial-with-examples/
c语言strlen函数