The program needs to be written in C language
17.4 Project 3 Word Processor
Project 3
In this project, you will design a simple word processor in C. Your word processor will take a line of input and align it left, right, or centered. It will also report word count and average word length.
To have an idea of what the end result will be, an example execution is shown below. The first four lines are prompts for input, the other lines are outputs of the program:
Input: CS Column width: 4 Alignment: center Autocorrect: no ---- CS Words: 1 Avg word length: 2.000000
All input will be less than 1000 characters. You can also assume all inputs are valid, except for column width, which may be an invalid value that you will detect.
More details are discussed in the following sections.
Alignment
The text to align is entered via the Input: prompt. Then the user will select a column width with the Column width: prompt and a text alignment via the Alignment: prompt.
The user will enter a column width, and you will align the text within this column. For example, if the entered text was 'CS' and the column width was 6, you would center it as:
Note the six hyphens output to show each column
------ CS
As seen above, each column number is shown, followed by a separating line of dashes, and then the final output.
In the above example, the text is padded with two spaces on each side to center it. The two spaces on the right are not needed and should not be output.
In the event that you find yourself with an odd number of spaces to divide, place the extra space on the left. For example, if the column width was 7:
------- CS
Examples of the three alignments given a column spacing of 6 are shown below:
Left
------ CS
Right
------
CS
Centered
------ CS
If the column width is less than the length of the input, your program should print out the follow text (ends with newline) and exit by returning 1:
Invalid column width
Additional Details
In addition to alignment, your program will also implement a rudimentary autocorrect feature, count the number of words, and report the average word length.
Autocorrect
Due to OCR errors, or in an attempt to avoid content filters, words may contain errors such as H3110 (H three one one zero) instead of Hello. Your program should provide the user the option to correct these errors. If the user chooses to correct them, you will:
When you prompt the user with Autocorrect:, they should be able to enter yes or no.
In addition to these replacements, when autocorrect is on, you should also capitalize the first character of every sentence. This will be the first character of the input and any character following . (period and a space). Finally, ensure the output ends with a period if it doesn't already. Example:
h3llo world. this is a sentence => Hello world. This is a sentence.
Here you will see the first h was capitalized, the 3 was replaced with an e, the t in this was capitalized, and a period was added to the end of the sentence.
If you add a period to the end, increase the column width by 1 as well to compensate for the increase in string length.
Word count and average length
Word count: For simplicity, we will define word count as the number of spaces plus 1.
Average word length: Output the average word length as %lf. A word's length is defined as the count of alphabet characters (a-z, A-Z) in the word.
Example
A complete example of how to handle input and display output is shown below:
Input: Hi world
Column width: 20
Alignment: center
Autocorrect: no
--------------------
Hi world
Words: 2
Avg word length: 3.500000Code Screenshots:





Sample Output:

Code to copy:
//Include the
//required header files.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
//Define the
//main() function.
int main() {
//Declare the
//required variables.
char line[1000];
int width;
char alignment[10];
char autocorrect[3];
int num_words = 0;
double avg_word_len;
int letter_count = 0;
int line_length;
char new_line_char;
//Prompt the user
//to enter the input.
printf("Input: ");
fgets(line, 1000, stdin);
//Prompt the user to
//enter the column width.
printf("Column width: ");
scanf("%d%c", &width, &new_line_char);
printf("Alignment: ");
fgets(alignment, 10, stdin);
printf("Autocorrect: ");
fgets(autocorrect, 4, stdin);
int capitalize = 1;
line_length = strlen(line);
line[line_length-1] = '\0';
line_length -= 1;
//If the length of
//the column is less
//than the length of the
//line then display an
//error message and return
//from the main() function
if(width < line_length)
{
printf("Invalid column width\n");
return 1;
}
//Run the loop to traverse the input text.
for(int i=0; i<line_length; i++)
{
//If the current
//character is a space then,
//increase the count
//of words by 1.
if(line[i] == ' ')
{
num_words++;
capitalize = 1;
}
//Otherwise, check if the
//text needs to corrected.
else if(capitalize == 1 && strcmp(autocorrect, "yes")==0)
{
line[i] = toupper(line[i]);
capitalize = 0;
}
if(isalpha(line[i]))
{
letter_count++;
}
}
//Perform the autocorrect
//operations if the
//autocorrection option
//is set to yes.
if(strcmp(autocorrect, "yes")==0)
{
if(line[line_length - 1] != '.')
{
strcat(line, ".");
line_length++;
width = width+1;
}
char ch = 'y';
while(ch != 'n')
{
printf("Do you wish to replace a character in the line(y/n)? ");
scanf("%c", &ch);
if(ch == 'n')
{
break;
}
else if(ch == 'y')
{
char replace;
char new_char;
printf("Enter the character to be replaced: ");
scanf("%c", &replace);
printf("Enter the new character: ");
scanf("%c", &new_char);
for(int i=0; i<line_length; i++)
{
if(line[i] == replace)
{
line[i] = new_char;
}
}
}
else
{
printf("Invalid input!!"
" Please enter \'y\'"
" or \'n\'\n");
}
}
}
if(line[line_length - 1] != ' ')
{
num_words++;
}
int i;
for(i=0; i<width; i++)
{
printf("-");
}
printf("\n");
if(strcmp(alignment, "left")==0)
{
printf("%s", line);
printf("\n");
}
else if(strcmp(alignment, "center")==0)
{
int num_spaces = (width - line_length)/2;
if(width%2 != 0)
{
num_spaces += 1;
}
int i;
for(i=0; i<num_spaces; i++)
{
printf(" ");
}
printf("%s", line);
printf("\n");
}
else
{
int i;
int num_spaces = (width - line_length);
for(i=0; i<num_spaces; i++)
{
printf(" ");
}
printf("%s", line);
printf("\n");
}
avg_word_len = (float)(letter_count)/(float)(num_words);
printf("Words: %d\n", num_words);
printf( "Avg word length: %lf", avg_word_len);
printf("\n");
return 0;
}
The program needs to be written in C language 17.4 Project 3 Word Processor Project 3...