length of char


I have a question about char.
how do you get the length of a char
for example:

input: hello
output: 5

I think this question is already asked but I didn't find it anywhere on the forum.
It depends on where characters are stored. If you use a character array then the number of actual characters stored in it is calculated with using standard C function strlen
If characters are stored in standard C++ container std::string then there is member function length() or its synonim size() that return the number of characters.

For example

char s[ 100 ] = "Hello";

size_t size = std::strlen( s );

std::cout << "size = " << size << std::endl;

std::string s1( "Hello" );

std::string::size_type size1 = s1.size();

std::cout << "size1 = " << size1 << std::endl;
or u can use a loop .It is a pathetic try since standard library functions like strlen as vlad from moscow explained already exists
txn it worked.
The length of a char is always 1 = p
the length of a string you can do
int lengthOfString = string.size();
cout << "Length of string is: " << lengthOfString << " characters." << std::endl;
Topic archived. No new replies allowed.