Question: How would I go about making a line editor in C with these requirements? This program shall contain no global variables. We assume that the

How would I go about making a line editor in C with these requirements?

This program shall contain no global variables.

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

You will probably need to split the problem into a hierarchy of many functions. A possible set of functions could include (but is not limited to):A function that trims the newline out of a string.

When fgets() reads from the user, it also takes in the newline and for this problem the newline has to be removed.

Hint: removing characters at the end of a string is easy if you just move the end-of-line character where you need it to be!

Such function should only trim the newline character if there is one in the first place.

A function that empties the input buffer.

When scanf() reads a character or a number, it leaves the newline character in the input buffer. If the program was to call fgets() right afterwards, fgets() would return immediately an empty string only containing the newline.

In order to prevent that, we need a function that discards all the characters left in the input buffer up until a newline character is found.

For this function, you probably need to use the function getchar() which returns the next character in the input buffer.

Call getchar() in a loop until the character is a newline.

A function that prompts the user for a command and returns it.

If you use scanf() for this function, dont forget to empty the input buffer afterwards.

A function that can locate a substring in a string and return its position.

Or return an error in case the substring could not be located;

A function that can delete a portion of a string, given a starting position and a number of characters to remove.

A function that can insert a substring in a string at a given position.

The main function should be the brain, first getting the initial string, then getting commands from the user performing them, using sub-functions, until the user wishes to quit.

List of some important libc functions that are used in the reference program: strlen(), getchar(), strncmp(), strcpy(), fgets(), strcat(), scanf().

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!