Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write program word_reverse.c that reverse all the words of a line, as illustrated in the following example: There are multiple ways of reversing a string

Write program word_reverse.c that reverse all the words of a line, as illustrated in the following example:

There are multiple ways of reversing a string but here you have to follow a specific algorithm. First, you have to count the number of words in the string; we assume that words are separate by one space and that no other punctuation exists. Then you have to create a array of strings, each item being one of the words. The last item is NULL to mark the end of the array. If item are ordered from the last word to the first word, recomposing the reserved string only consists in concatenating these words in sequence.

Here are a list of requirements, assumptions and hints:

This program shall contain no global variables.

We assume that the maximum number of characters a line can contain is 80.

While the input string that is being filled out by the user is a local array in the main function, any additional strings created during the execution should be dynamically allocated in the heap.

Do not forget that dynamically allocated memory should be freed appropriately.

You must implement function char **tokenize_words(char *line) that returns a dynamically allocated array of strings containing all the words in reverse order.

Hint: you should start iterating from the end of the array when filling it out.

The last item of the array should be initialized to NULL so that you can recognize the end of the array when iterating.

You will probably need to split a few helper functions for this problem. A possible set of functions could include (but is not limited to):

A function that counts the number of words in the line. Words are separated by spaces and the line probably ends with a newline character (look into the functions from ctype.h).

You will need this function to allocate the first dimension of your array of strings.

A function that counts the number of character in a word.

You will need this function to allocate each string in your array of strings.

A function that skips leading spaces from a certain string and returns a pointer to the first non-space character.

When scanning the input string and split it into separate words, you will need this function to go to the next word.

The main function should get and echo the user input, then get the reversed string as per described by the assignment and display it.

List of some important libc functions that are used in the reference program: malloc(), free(), strncpy(), strcat(), fgets().

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

Step: 3

blur-text-image

Ace Your Homework with AI

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

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

5. Identify three characteristics of the dialectical approach.

Answered: 1 week ago

Question

6. Explain the strengths of a dialectical approach.

Answered: 1 week ago

Question

4. Explain the strengths and weaknesses of each approach.

Answered: 1 week ago