Answered step by step
Verified Expert Solution
Link Copied!
Question
1 Approved Answer

How to write this in C program: For this assignment, we want to write a function that formats/justifies a string. The signature of the function

How to write this in C program:

For this assignment, we want to write a function that formats/justifies a string. The signature of the function should look like:

void formatAndPrintParagraph(char *paragraph, int lineLength);

Very important: You should make sure to use this exact signature.

Lets look at some examples first!

1) paragraph = "Hi everyone. This is the 2nd assignment. Please make sure you start early as this is going to take some time!"

lineLength = 25

output should be:

2) paragraph = "Hi everyone. This is the 2nd assignment. Please make sure you start early as this is going to take some time!"

lineLength = 40

output should be:

So, what is this output? Basically, we want to break the paragraph into lines of equal length (parameter lineLength). We also want to make sure the number of spaces we add in between words are balanced. For example if your line length is 30 and on your first line you have four words with sizes: 7, 4, 8 and 3 you need to add 3 spaces between first word and second word, three spaces between second word and third word and finally two spaces between third word and fourth word. Your lines length would add up to:

7 (first word) + 3 spaces + 4 (second word) + 3 spaces + 8 (third word) + 2 spaces + 3 (fourth word) = 30.

Here are some assumptions you can make:

Maximum number of words in the paragraph = 100

Maximum length of a word = 20

Hint!

I wrote this program myself. Here are the functions that I have in my code. Please note that I am providing these as help for you to start your program. You dont necessarily need to have these exact functions:

#define MAX_WORD_LENGTH 20

#define MAX_NUMBER_OF_WORDS 100

char *moveToBeginningOfNextToken(char *inputString);

int getCurrentTokenSize(char *inputString);

int tokenize(char * paragraph, char tokens[MAX_NUMBER_OF_WORDS][MAX_WORD_LENGTH]);

int getNumberOfWordsForNextLine(char tokens[MAX_NUMBER_OF_WORDS][MAX_WORD_LENGTH], int numberOfWordsProcessedSoFar, int totalNumberOfWords, int lineLength);

void printWordAndSpaces(char word[MAX_WORD_LENGTH], int numberOfSpaces);

void formatAndPrintCurrentLine(char tokens[MAX_NUMBER_OF_WORDS][MAX_WORD_LENGTH], int numberOfWordsProcessedSoFar, int numberOfWordsOnNextLine, int lineLength);

void formatAndPrintWords(char tokens[MAX_NUMBER_OF_WORDS][MAX_WORD_LENGTH], int totalNumberOfWords, int lineLength);

void formatAndPrintParagraph(char *paragraph, int lineLength);

Lets see what each of these is supposed to do:

  • moveToBeginningOfNextToken: Takes a string and moves the pointer to the first non-blank character (or end of the string if there is no non-blank character).

  • getCurrentTokenSize: given a string with no leading spaces, returns the size of the first word. For example, if the string is Hello world, it should return 5.

  • tokenize: given the paragraph, it breaks it down into words. Stores the words in the given tokens array of string. For example, for paragraph = Hi. How is it going? Thanks. This function would fill the tokens array to be like this: tokens[0] = Hi., tokens[1] = How, tokens[3] = is and so on.

  • getNumberOfWordsForNextLine: Given the array of tokens (words), number of words that we have processed so far, total number of words (size of the string array) and line length (from the formatAndPrintParagraph function): It returns the number of words that needs to be printed on the next line.

  • printWordAndSpaces: This function simply prints a string followed by some number of spaces (no new line).

  • formatAndPrintCurrentLine: Given the tokens/words, number of words printed so far, number of words that needs to be printed on the next line and line length, this function basically prints one line of the output.

  • formatAndPrintWords: You would call this function after you have tokenized the input paragraph. This number basically formats words in a string array (tokens) on lines of length lineLength.

  • formatAndPrintParagraph: The function that this assignment is asking for!

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image
Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students explore these related Databases questions

Question

3. What strategies might you use?

Answered: 3 weeks ago