If your definition of empty char array sets all elements of the array to zero, you can use std::memset .
This will allow you to write this instead of your clear loop:
const size_t arraySize = 20;
Regarding the "strange" results of strlen() :
strlen(str) returns "(...) the number of characters in the character array that str points to the first element up to the first zero character. This means that it will count the characters until it finds zero.
Check the contents of the lines you pass to strlen() - you may have white characters (e.g. \r or \n , for example) that were read from the input stream.
Also, tip - consider std::string instead of regular char arrays.
A memset() note: memset() often optimized for high performance. If this is not your requirement, you can also use std::fill , which is a more C ++-like way to populate an array of everything:
char testName[arraySize]; std::fill(std::begin(testName), std::end(testName), '\0');
std::begin() (and std::end ) works well with compile-time arrays.
Also, to respond to @SergeyA's comment, I suggest reading this SO post and this answer .
source share