本文共 5050 字,大约阅读时间需要 16 分钟。
c ++中字符串长度的
The string length in C++ can be calculated or found by various methods. Here, in this tutorial, we are going to learn all of them. And we would also implement the same through programming in C++.
C ++中的字符串长度可以通过各种方法计算或找到。 在这里,在本教程中,我们将学习所有这些。 而且我们还将通过C ++编程实现相同的功能 。
The different methods following which we are going to calculate the length of a given string are listed below:
下面列出了用于计算给定字符串长度的不同方法:
size()
size()
length()
length()
strlen()
strlen()
The string.h header file in C++ comes with both the size()
and length()
functions pre-defined within itself. Often the two functions are used interchangeably and even perform in a similar manner returning the same values.
C ++中的string.h头文件在其内部预定义了size()
和length()
函数。 通常这两个函数可以互换使用,甚至以相似的方式返回相同的值。
And on the other hand the cstring.h header file has strlen()
function pre-defined in it.
另一方面, cstring.h头文件具有预定义的strlen()
函数。
Interestingly, we can also find the length of a string in C++ by using a counter inside a loop structure. The loop iterations may be performed by either for
or while
.
有趣的是,我们还可以在C ++中找到字符串的长度 通过在循环结构内使用计数器。 循环迭代可以通过for
或while
。
Certainly, we cannot use the do-while
loop because of the fact that it iterates for a minimum of 1 time. So in a case where the string does not have any characters, the do-while loop would return 1 instead of 0.
当然,我们不能使用do-while
循环,因为它至少迭代了1次。 因此,在字符串不包含任何字符的情况下,do-while循环将返回1而不是0。
So, let us see how we can all the above-mentioned methods to calculate the length of a string.
因此,让我们看看如何使用上述所有方法来计算字符串的长度。
The size()
function in C++ returns the length of a specified set (String in our case) in terms of bytes. It is defined in the string.h header file in C. The code below depicts how the size()
function can be used:
C ++中的size()
函数以字节为单位返回指定集合(在我们的情况下为String)的长度。 它在C的string.h头文件中定义。以下代码描述了如何使用size()
函数:
#include#include using namespace std;int main(){ string a; cout<<"Enter the string: "; getline(cin,a); cout<<"\nThe size of the string is: "<
Output:
输出:
The length()
function is also defined inside the string.h header file and works in the same way size()
does. Both functions return the same values and are also used in the same way. Let us look at the example below:
length()
函数也定义在string.h头文件中,并且与size()
相同。 这两个函数返回相同的值,并且也以相同的方式使用。 让我们看下面的例子:
#include#include using namespace std;int main(){ string a; cout<<"Enter the string: "; getline(cin,a); cout<<"\nThe size of the string usnig length() is: "<
Output:
输出:
strlen()
is yet another function that is extensively used for calculating the length of a given cstring (Or an array of characters). It is defined in the cstring.h header file. Let us look at the example given below:
strlen()
是另一个函数,广泛用于计算给定cstring(或字符数组)的长度。 它在cstring.h头文件中定义。 让我们看下面的例子:
#include#include #include using namespace std;int main(){ char string[50]; cout<<"Enter the string: "; fgets(string,50,stdin); cout<<"\nThe size of the string usnig strlen() is: "<
Output:
输出:
As we can see, we get the length of the given string as our output. Since the fgets()
function also considers the newline character inside the string, the length of the string returned by the strlen()
function has to be decreased by 1.
如我们所见,我们获得给定字符串的长度作为输出。 由于fgets()
函数还在字符串内考虑换行符,因此strlen()
函数返回的字符串长度必须减少1 。
The example below illustrates how using loop iterations we can count the length of a specified string.
下面的示例说明了如何使用循环迭代来计算指定字符串的长度。
#include#include #include using namespace std;int main(){ string a; int count=0; cout<<"Enter the string: "; getline(cin,a); while(a[count]!='\0') { count++; } cout<<"\nThe size of the string using loop is: "<
Output:
输出:
The count variable is incremented by 1 on each successful iteration of the while loop until the end of the string or the NULL character('\0'
) is encountered. Consequently, we get the total number of elements present in the string. Which actually is the length of the string itself.
在while循环的每次成功迭代中, count变量将增加1 ,直到遇到字符串结尾或NULL字符( '\0'
)。 因此,我们获得了字符串中存在的元素总数。 实际上是字符串本身的长度。
All the different methods used in this tutorial help us find string length in C++ where the size()
and length()
functions are used while handling C++ string objects. Whereas, strlen()
is applicable for C-type strings only. Loop iterations can be used for both cases.
本教程中使用的所有不同方法都可以帮助我们在C ++中查找字符串长度,其中在处理C ++字符串对象时使用size()
和length()
函数。 而strlen()
仅适用于C型字符串。 两种情况都可以使用循环迭代。
翻译自:
c ++中字符串长度的
转载地址:http://pwlzd.baihongyu.com/