I have a question that reads as follows:
Write a function int tokenCopy(char* dest, const char* src, int destSize) which copies characters from the given source string src into the given destination buffer dest, which is of size destSize, until either:
the end of the source string occurs, or
the destination buffer is full (allowing for the terminator that will be needed), or
a space character is found in the input string
whichever comes first. If the copying finishes because a space is found, the space is not copied. The destination string must always be correctly terminated. If there is insufficient space in the destination for the source string, the destination string must be a correctly terminated leading substring of the source string.
The return value is the number of characters copied, not including the terminating null byte.
This is my attempt so far:
int tokenCopy(char* dest, const char* src, int destSize)
{
int the_slab = 0;
for (; *src != ('\0' | ' '); the_slab++)
{
if ( *src == (32 | 0))
{
*dest = '\0';
return the_slab;
}
*dest = *src;
dest++;
src++;
}
*dest = '\0';
return the_slab;
}
However it fails when tested by:
char buff[10];
int n = tokenCopy(buff, "This", 10);
printf("%d '%s'\n", n, buff);
Because it returns the number 7 as opposed to 4. why is it not terminating after it's processed the first four letters? I don't understand why src doesn't have a terminating byte?
What have I done wrong? What don't I understand conceptually?
Thanks!