Hi i am trying to limit the length of a string to output on console. So that only a max of 78 char can be displayed on one line. If it exceeds that length the last word is not added to the line but moves to the next.
This is my code so far.
Code:
char delims[] = " ";
char *tokens;
char stringLine[78] = "";
char stringLine2[78] = "";
char temp;
tokens = strtok(inputStr, delims);
while ((strlen(stringLine) + strlen(tokens)) <= 77 && tokens != NULL) {
strncat(stringLine, tokens, 78);
strncat(stringLine, " ", 78);
tokens = strtok(NULL, delims);
}
printf("%s", stringLine);
while ((strlen(stringLine2) + strlen(tokens)) <= 77 && tokens != NULL) {
strncat(stringLine2, tokens, 78);
strncat(stringLine2, " ", 78);
tokens = strtok(NULL, delims);
}
printf("%s", stringLine2);
This works fine unless the tokens == NULL then the program crashes.