0
votes

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.

4
what about the first for loop?Sourav Ghosh

4 Answers

3
votes

You need to assign to dest inside the function

int tokenCopy(char *dest, const char *src, int destSize) {
    *dest = 0;
    // rest of the code
}
0
votes

After getting rid of the first for loop, you can modify the second for loop to something like

for ( i = 0; *src != '\0' && *src!= ' ' && (i < destSize-1) ; i++)
{
   *dest++ = *src++;
}
*dest = '\0';

return new_value;

to get what you want. The modification includes

  • getting rid of the redundant check on empty string.
  • combining the copying of value and the pointer increment
  • adding the destination length check as a terminating condition for the loop itself
0
votes

Even the very first loop of your function implementation

    for (;*src != ' '; new_value++){
    destSize = 1;
    return new_value;
    }

does not make sense.

The function can be written the following way

#include <stdio.h>

size_t tokenCopy( char *dest, const char *src, size_t destSize )
{
    size_t n = 0;

    if ( destSize != 0 )
    {        
        while ( n < destSize && *src && *src != ' ' ) dest[n++] = *src++;

        dest[n == destSize ? --n : n] = '\0';
    }        

    return n;
}

int main( void )
{
    char buff[10];
    size_t n;

    n = tokenCopy( buff, " ", 10 );
    printf("%zu \"%s\"\n", n, buff );    

    n = tokenCopy( buff, "0123456789", 10 );
    printf("%zu \"%s\"\n", n, buff );    
}    

The program output is

0 ""
9 "012345678"
0
votes
#include<stdio.h>

int tokenCopy(char* dest, const char* src, int destSize){
        while(*src && destSize>1 && *src!=' ') {

                *dest = *src;

                dest++;
                src++;
                destSize--;
        }

        *dest = '\0';


}

main(int argc, char **argv)
{
        char buff[6];
        tokenCopy(buff,"Th",6);

        printf("%s\n",buff);
}