int func(char* string, char from, char to)
{
int result = 0;
if(!string) return 0;
// ...
}
i don't know whether this if-statement is checking, that the given string is empty or NULL. I tried to check it but I didn't receive a clear answer.
int func(char* string, char from, char to)
{
int result = 0;
if(!string) return 0;
// ...
}
i don't know whether this if-statement is checking, that the given string is empty or NULL. I tried to check it but I didn't receive a clear answer.
Your code checks whether string is NULL. If so, it returns 0. To check whether string is empty, you could do either of the following:
if (strlen(string) == 0)
{
printf("String is empty.\n");
return -1;
}
or you can do:
if (*string == '\0')
{
printf("String is empty.\n");
return -1;
}
strlen() way should only be used when the string is NULL terminated.
if (!string)
is equivalent to:
if (string==0)
and so it tests whether the parameter string
that is passed is pointing to an object. Whether that object is a string (a sequence of characters terminated with a null character) it cannot check.
It is a bit confusing as logically it say 'if not string' but instead of it meaning if this is not a string it actually means if this string doesn't exist ie if it is null.