Hey, i'm trying to learn C and am stuck trying to answer a simple question. ( Copying sequence of chars )
My Problem is that my program is printing out a random symbol afterwards , when trying to copy a sequence of chars into a new buffer.
Code:
#include <stdio.h>#include <stdlib.h>
int tokenCopy(char* dest, const char* src, int destSize);
int main()
{
char buff[3];
int n = tokenCopy(buff, "This is a string", 3);
printf("%d '%s'\n", n, buff);
return EXIT_SUCCESS;
}
int tokenCopy(char* dest, const char* src, int destSize){
int num_chars = 0;
for(int i = 0; i < destSize; i++){
if(src[i] == '\0' || src[i] == ' ' ){
break;
}
dest[i] = src[i];
num_chars+= 1;
}
return num_chars;
}
Could someone tell me what i need to change? I think its something to do with null terminator?