I am currently working on a computer science project and have gotten it totally working bar a very specific case which I cannot seem to fix. The goal of the code is to 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.
Where testing in done in the form of:
char buff[3];
int n = tokenCopy(buff, "This is a string", 3);
printf("%d '%s'\n", n, buff);
This is my code so far:
#include <stdio.h>
int tokenCopy(char* dest, const char* src, int destSize){
int new_value = 0;
int i=0;
for (;*src != ' '; new_value++){
destSize = 1;
return new_value;
}
for (; *src != '\0' && *src!= ' '; new_value++)
{
while (i < destSize-1 && *src!= '\0'){
*dest = *src;
dest++;
src++;
new_value++;
i++;
*dest = '\0';
}
return new_value;
}
return new_value;
}
However in the case of the following test:
char buff[10];
int n = tokenCopy(buff, " ", 10);
printf("%d '%s'\n", n, buff);
The output should be:
0 ''
However my code is outputting the zero, followed by the speech marks containing a different letter followed a question mark in a diamond per each change I try to make to the code. For reference I am using linux with the Geany IDE.
Any help with this problem would be much appreciated.