Question: 0 I have an assignment that basically is asking to justify a paragraph given line length. So for instance the paragraph I am a student

0

I have an assignment that basically is asking to justify a paragraph given line length. So for instance the paragraph "I am a student of C, this is my first assignment. I hope I finish on time." given line length of 17 should be as follows:

output I am a student of C, this is my first assignment. I hope I finish on time. 

I am having trouble with dynamically placing spacing in between the words. I currently have a function that counts the words in a paragraph and stores them into a 2d array but I have no idea how to a) calculate the amount of spacing in between words and b) how to dynamically print that justified paragraph. Here is the code I have so far:

int getAllWordsFrom2DArray(char *paragraph, char words[MAX_NUMBER_OF_WORDS][MAX_WORD_LENGTH]) { int i,j,totalWords = 0; for(i=0; i < strlen(paragraph); i++) { int wordLength; if (paragraph[i] == ' ' || paragraph[i+1] == '\0') { totalWords++; wordLength = i; for(j=0; j < wordLength; j++) { words[i][j] = paragraph[j]; } } } printf("%s", words); return totalWords; } //Code in progress int getNumberOfWordsForNextLine(int totalWords, int lineLength, char words[MAX_NUMBER_OF_WORDS][MAX_WORD_LENGTH]) { int wordsForNextLine = 0; for(int i=0; i < totalWords; i++) { wordsForNextLine = 0 ; } } //code in progress void printNextLine(int wordsForNextLine) { } //skeleton code provided by instructor void justifyAndPrintParagraph(char* paragraph, int lineLength) { char words[MAX_NUMBER_OF_WORDS][MAX_WORD_LENGTH]; int totalWords = getAllWordsFrom2DArray(paragraph, words); int processedWords = 0; while (processedWords < totalWords) { int wordsForNextLine = getNumberOfWordsForNextLine(totalWords, lineLength, words); printNextLine(wordsForNextLine); processedWords += wordsForNextLine; } } 

To clarify, we are not allowed to use strlok. Essentially we are expected to just use the basics in doing this. I need to use the void justifyAndPrintParagraph function and signature but other than that I'm free to do whatever.

Edit: I forgot to add that if spaces cannot be evenly divided then the extra spaces are to be allocated left to right.

Any help is greatly appreciated.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!