如何在C++中找到单词的长度?

How to find the lenght of the word in C++?

我们有一些词,我们应该确定其长度。我怎样才能做到这一点? 输入示例:“你好”——不带引号; 示例输出:5

C++ 中有一个计算长度的函数。 您可以尝试使用:

input.length()

另请记住,您需要包括:

#include <iostream>
using namespace std;

参考此文档: https://www.w3schools.com/cpp/cpp_strings_length.asp

您可以使用 input.length() or input.size().

或者您可以使用这个简单的循环。

 for (i = 0; str[i]; i++) 
        ; 
    cout << i << endl; 

如果输入包含在 std::string 中,您可以找到 Ravi 规定的长度。 如果它是一个 C 字符串,你可以用

找到长度
int len = strlen(INPUT);

在C/C++中,大写通常用于常量,因此最好将字符串命名为input,而不是INPUT.

string str;
cin>>str;
//use this
cout<<str.length();
//or use this
cout<<str.size();

两者都可以正常工作。