Hi, I wrote a c file to output text, making sure it doesn't go over a maximum width:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "printer.h"
/* Print the given text to stdout, adding newlines as
* necessary between words in order to keep the maximum line
* length to the given specified value.
*/
void printText(const char *text, const int maxLineWidth) {
int lineWidth = 0;
int textEnd = 0;
int i = 0;
char currWord[strlen(text)+1];
while (textEnd == 0) {
int k = 0;
while (isspace(text[i]) == 0) {
if (text[i] == '\0') {
textEnd = 1;
break;
}
currWord[k++] = text[i++];
}
currWord[k] = '\0';
if ((strlen(currWord) + lineWidth) <= maxLineWidth) {
printf("%s", currWord);
lineWidth += strlen(currWord);
}
else {
printf("\n%s", currWord);
lineWidth = strlen(currWord);
}
}
printf("\n");
}
int main() {
printDescription("Hello, this is some text aligned to a maximum width of 15 characters.", 15);
return 0;
}
instead of printing as it's supposed to it just hangs, waiting with no output, i assume it's to do with a loop condition and that its looping forever but i can't spot where and figure how to fix it. please help me out, thanks.
great board by the way, glad to be a part.