Possible Duplicate:
C++ String Length?
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.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can use
strlen(mystring)from<string.h>. It returns the length of a string.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’
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>.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:
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/
In this case you can declare a string and get its length the following way: