My professor is having us make our own version of the functions mystrcat, mystrlen and mystrcopy. I have a problem where the word is returning the wrong amount of bytes. Or, rather, I think it is.
The phrase: "HELLO WORLD!"
, if I am not wrong, should return 13 bytes? Like in:
const char char_literal[] =
{ 'H', 'E', 'L', 'L', 'O', ' ', 'W', 'O', 'R', 'L', 'D', '!', '\0' };
// 1 2 3 4 5 6 7 8 9 10 11 12 13
I wrote the function myStrLen()
that returns ({ 'H', 'E', 'L', 'L', 'O', ' '}
) as 6
, but because it is alone, there should be a '\0'
at the end right?
So should I be returning i + 1
or is 6
correct?
int myStrLen(char stringInput[]){
for(int i = 0; ; i++){
if(stringInput[i] == '\0'){
return i;
If your professor tells you to write a function that behaves exactly as strlen
, then it shall not count the string termination character:
std::size_t strlen( const char* str );
Returns the length of the given byte string, that is, the number of characters in a character array whose first element is pointed to by str up to and not including the first null character. ...
So returning i
is correct, besides the fact that the "original" strlen
takes a const input parameter and returns a size_t
...