Hi there, like many other first posters on the forum, I'm a programming n00b,
For practice, I'm trying to write a function that copies chars from a source string into a buffer until either: there's a space, the string ends or the buffer is full. I have yet to code for the last condition (read: unsure how to approach it)
This is my code so far:
Code:
#include<stdio.h>
int tkenCpy (char*dest, const char *src, int destSize) {
int main () {
char buff[5];
int n = tkenCpy(buff, "This is a string", 5);
printf("%d'%s'\n", n, buff);
return 0;
}
int tkenCpy (char*dest, const char *src, int destSize) {
int i = 0;
int chcpy = destSize -1;
for(i=0; i < chcpy; i+=1) {
if(*src != '\0' && *src != 32) {
dest[i ]= *(src+i);
}
}
return *dest;
}
As shown in the main function, I'd like my output to be the number of chars printed, then print the copied string. But my output is as follows:
The '?~' are two random characters. I'm not sure what the 84 refers to, and it stays the same for any array size... buff[3] etc. Also, the final apostrophe demanded by the printf function is not there.
Any help would be greatly appreciated