Following is the program I encountered
#include <stdio.h>
void sp_to_dash(const char *str);
int main(void)
{
sp_to_dash("this is a test");
return 0;
}
void sp_to_dash(const char *str)
{
while(*str) //beginning of while loop
{
if(*str== ' ')printf("%c", '-');
else printf("%c", *str);
str++;
}
}
At the beginning of while loop we can see that while(*str) is initiated without any condition, which means it is not mentioned that when the *str should stop on null or '\0'. The code is working fine, however according to me it should be like this while(*str != null) or while(*str != '\0'). Please explain this one to me.
while(*str != null){...}clearly indicates that don't have clear the difference between empty strings, null pointers and theNULASCII character'\0'(and BTW the null pointer isNULL, notnull). It's a disease somewhat common among C newbies, but you should fix that as soon as possible.