博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c ++中字符串长度的_C ++中的字符串长度
阅读量:2532 次
发布时间:2019-05-11

本文共 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 ++编程实现相同的功能

在C ++中查找字符串长度的方法 (Methods to find String Length in C++ )

The different methods following which we are going to calculate the length of a given string are listed below:

下面列出了用于计算给定字符串长度的不同方法:

  1. size()

    size()
  2. length()

    length()
  3. strlen()

    strlen()
  4. using loops.

    使用循环

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 ++中找到字符串的长度 通过在循环结构内使用计数器。 循环迭代可以通过forwhile

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.

因此,让我们看看如何使用上述所有方法来计算字符串的长度。

1. C ++中的size() (1. size() in C++)

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:

输出:

String length in C++ Using Size()
Using size()
使用size()

2. length()在C ++中查找字符串长度 (2. length() to Find String length in C++)

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:

输出:

Using length()
String Length Using length()
使用length()的字符串长度

3. C ++中的strlen() (3. strlen() in C++)

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:

输出:

Using strlen()
String Length Using strlen()
使用strlen()的字符串长度

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

4.使用循环在C ++中查找字符串长度 (4. Using loops to find string length in C++)

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:

输出:

Using Loops
String Length Using Loop
使用循环的字符串长度

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' )。 因此,我们获得了字符串中存在的元素总数。 实际上是字符串本身的长度。

结论 (Conclusion)

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型字符串。 两种情况都可以使用循环迭代。

参考资料 (References)

翻译自:

c ++中字符串长度的

转载地址:http://pwlzd.baihongyu.com/

你可能感兴趣的文章
转:How to force a wordbreaker to be used in Sharepoint Search
查看>>
MySQL存储过程定时任务
查看>>
Python中and(逻辑与)计算法则
查看>>
POJ 3267 The Cow Lexicon(动态规划)
查看>>
设计原理+设计模式
查看>>
音视频处理
查看>>
tomcat 7服务器跨域问题解决
查看>>
前台实现ajax 需注意的地方
查看>>
Jenkins安装配置
查看>>
个人工作总结05(第二阶段)
查看>>
Java clone() 浅拷贝 深拷贝
查看>>
深入理解Java虚拟机&运行时数据区
查看>>
02-环境搭建
查看>>
spring第二冲刺阶段第七天
查看>>
搜索框键盘抬起事件2
查看>>
阿里百川SDK初始化失败 错误码是203
查看>>
透析Java本质-谁创建了对象,this是什么
查看>>
BFS和DFS的java实现
查看>>
关于jquery中prev()和next()的用法
查看>>
一、 kettle开发、上线常见问题以及防错规范步骤
查看>>