📅  最后修改于: 2020-09-25 09:11:22             🧑  作者: Mango
size_t strlen( const char* str );
strlen()
将以空终止的字节字符串 str
作为参数,并返回其长度。该长度不包含空字符 。如果字符串没有空字符 ,则该函数的行为是不确定的。
它在
str
:指向要终止长度的空终止字节字符串的指针。
strlen()
函数返回以null结尾的字节字符串的长度。
#include
#include
using namespace std;
int main()
{
char str1[] = "This a string";
char str2[] = "This is another string";
int len1 = strlen(str1);
int len2 = strlen(str2);
cout << "Length of str1 = " << len1 << endl;
cout << "Length of str2 = " << len2 << endl;
if (len1 > len2)
cout << "str1 is longer than str2";
else if (len1 < len2)
cout << "str2 is longer than str1";
else
cout << "str1 and str2 are of equal length";
return 0;
}
运行该程序时,输出为:
Length of str1 = 13
Length of str2 = 22
str2 is longer than str1