Jan 14, 2018 at 10:24pm UTC
How do you expect the loop to find a '\0' when the array doesn't contain such a character.
Jan 14, 2018 at 10:30pm UTC
How do you expect the loop to find a '\0' when the array doesn't contain such a character.
\0 is exist in the array, it's the termination character.
if i was wrong, can you explain please ?
Last edited on Jan 14, 2018 at 10:31pm UTC
Jan 14, 2018 at 11:24pm UTC
...and to clarify further, you're thinking of string literals. They will put a null terminator on the end. But you have to give it enough room (the actual length of the string + 1).
1 2
char a[6] = "hello" ;
// a --> { 'h', 'e', 'l', 'l', 'o', '\0' }
You're also trying to implement strlen, it seems.
http://www.cplusplus.com/reference/cstring/strlen/
Oh yeah, and... since you're using C++, you should use
std::string objects instead.
http://www.cplusplus.com/reference/string/string/length/
1 2 3 4 5 6 7 8 9 10
// string::length
#include <iostream>
#include <string>
int main ()
{
std::string str ("Test string" );
std::cout << "The size of str is " << str.length() << " bytes.\n" ;
return 0;
}
Last edited on Jan 14, 2018 at 11:26pm UTC