Don't use raw arrays!
Use std::array instead:
#include <array>
#include <iostream>
int main()
{
std::array<char,20> arr;
std::cout << "Length: " << arr.size() << std::endl;
}
There is literally no benefit in using raw arrays now a day in C++ (except maybe compatibility with C++03)
EDITED:
Now, the case is when I give input of less than 20 characters. (Eg.- "hello there" is of 11 characters). How to determine the exact length that we have typed?
Here we have several questions hidden:
- How to make a variable-length (runtime) char array to get input from the user?
- How to get the length of a string?
How to make a variable-length (runtime) array?
For non-char, containers like std::vector would help. In the case of a string, an ever better option is std::string.
Also, using streams to get the input from the user, most of those problems are solved.
How to get the length of a raw string?
Understanding raw-string as a sequence of char on a buffer, ended with an \0 nil character.
With std::string_view, we can use most of the std::string features on a raw char array:
#include <array>
#include <iostream>
#include <string_view>
int main()
{
std::array<char,20> arr{"This is a string"};
std::string_view strView(arr.data());
std::cout << "'" << strView << "' of length " << strView.size() << std::endl;
return 0;
}
Which output: 'This is a string' of length 16