Hi,
I am in my second year of Computer Science and have just been introduced to C this term. Coming from a JAVA background I must say it is slightly daunting, but I'm sure I'll pick it up...
I'm just having a bit of trouble with a small function I'm writing:
Code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
/*
* Print a given description to the standard output stream
* formatting the output to conform to the maxLineWidth.
*/
void printDescription(const char *description, const int maxLineWidth) {
char buff[maxLineWidth + 1], *b;
char word[maxLineWidth + 1], *w;
b = buff;
w = word;
for(; *description !='\0'; description++) {
*w = *description;
if (*description == ' ' && strnlen(w, maxLineWidth) + strnlen(b, maxLineWidth) <= maxLineWidth) {
b = strncat(*b, *w, strnlen(w, maxLineWidth));
w = "";
}
if (strnlen(w, maxLineWidth) + strnlen(b, maxLineWidth) > maxLineWidth) {
printf("%s\n", buff);
b = "";
b = strncat(*b, *w, strnlen(w, maxLineWidth));
w = "";
}
w++;
}
printf("%s\n", buff);
}
Basically the function 'printDescription' takes a paragraph of writing and outputs it depending on the 'maxLineWidth' parameter.
The way I am trying to implement it is by copying each character to a 'word', then when the word is complete placing it in the buffer for the line. Once the line has reached its maximum allowed length, the line is printed and then the next line and so on...
I keep getting an error for 'implicit declaration of strnlen' when I try to compile it, so I can't even see if it works yet. Any other tips on where my program is incorrect would also be welcome...