C++ 查找字符串的长度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11893753/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Find length of string
提问by Patrick Wills
Possible Duplicate:
C++ String Length?
可能重复:
C++ 字符串长度?
I really need a help now. How to accept string as input and find the length of the string? I just want a simple code just to know how it works. Thanks.
我现在真的需要帮助。如何接受字符串作为输入并找到字符串的长度?我只想要一个简单的代码来了解它是如何工作的。谢谢。
采纳答案by NotGaeL
You can use strlen(mystring)
from <string.h>
. It returns the length of a string.
您可以使用strlen(mystring)
从<string.h>
. 它返回字符串的长度。
Remember: A string in C is an array of chars which ends in character '\0'. Providing enough memory is reserved (the whole string + 1 byte fits on the array), the length of the string will be the number of bytes from the pointer (mystring[0]) to the character before '\0'
请记住:C 中的字符串是以字符 '\0' 结尾的字符数组。如果保留足够的内存(整个字符串 + 1 个字节适合数组),字符串的长度将是从指针 (mystring[0]) 到 '\0' 之前的字符的字节数
#include <string.h> //for strlen(mystring)
#include <stdio.h> //for gets(mystring)
char mystring[6];
mystring[0] = 'h';
mystring[1] = 'e';
mystring[2] = 'l';
mystring[3] = 'l';
mystring[4] = 'o';
mystring[5] = '#include <iostream>
#include <string>
string mystring ("hello"); //declares a string object, passing its initial value "hello" to its constructor
cout << mystring.length(); //outputs 5, the length of the string mystring
cin >> mystring; //reads a string from standard input. See http://www.cplusplus.com/reference/string/operator%3E%3E/
cout << mystring.length(); //outputs the new length of the string
';
strlen(mystring); //returns 5, current string pointed by mystring: "hello"
mystring[2] = 'std::string str;
std::cin >> str;
std::cout << str.length();
';
strlen(mystring); //returns 2, current string pointed by mystring: "he"
gets(mystring); //gets string from stdin: http://www.cplusplus.com/reference/clibrary/cstdio/gets/
http://www.cplusplus.com/reference/clibrary/cstring/strlen/
http://www.cplusplus.com/reference/clibrary/cstring/strlen/
EDIT: As noted in the comments, in C++ it's preferable to refer to string.h as cstring, therefore coding #include <cstring>
instead of #include <string.h>
.
编辑:如评论中所述,在 C++ 中,最好将 string.h 称为 cstring,因此编码#include <cstring>
而不是#include <string.h>
.
On the other hand, in C++ you can also use C++ specific string library which provides a string class which allows you to work with strings as objects:
另一方面,在 C++ 中,您还可以使用 C++ 特定的字符串库,它提供了一个字符串类,允许您将字符串用作对象:
http://www.cplusplus.com/reference/string/string/
http://www.cplusplus.com/reference/string/string/
You have a pretty good example of string input here: http://www.cplusplus.com/reference/string/operator%3E%3E/
您在这里有一个很好的字符串输入示例:http: //www.cplusplus.com/reference/string/operator%3E%3E/
In this case you can declare a string and get its length the following way:
在这种情况下,您可以通过以下方式声明一个字符串并获取其长度:
#include <iostream>
#include <string>
std::string s;
std::cin >> s;
int len = s.length();
回答by Luchian Grigore
Hint:
暗示:
##代码##回答by marcinj
in c++:
在 C++ 中:
##代码##