Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C programming.: Justified Write a function called justifyLeft() that justifies a

ID: 3776805 • Letter: C

Question

C programming.: Justified

Write a function called justifyLeft() that justifies a string left and a function called justifyRight() that justifies a string right. The function signatures are given next: int justifyLeftfconst char() src, char dest[], int maxsize); int justifyRightfconst char[] src, char dest[], int maxsize); String src is the source string, string dest is the destination buffer that holds the result of the operation, and argument maxsize is the capacity of the dest buffer. Text justification involves trimming spaces ' ' or adding spaces at one of the 2 ends of a string. For illustration, string "Hello" justified left to a destination buffer of size 10 should be "Hello " and the same string justified right should be " Hello". Notice that space ' ' characters are either cut or added, depending on justification. Remember that one char from the dest buffer must be reserved for the '' terminator. Consider that it is possible that sir may have more non-space characters that maxsize. In this case you have to limit the length of the string that is copied to the destination buffer in order to prevent buffer overflow. Write in a file p3.c functions justifyLeft and justifyRight. Write in file p3.c a main() function that reads from the terminal in a loop a text line using the fgets() function and then uses the justifyLeft() function to display the text in the line justified left and the justifyRight() function to display the text in the line justified right. Use a destination char buffer of capacity 20. The loop should end at the end-of-file (user types CTRL-Z or CTRL-D) OR when the user enters the word "quit". To read one line of text (with max. TEXTSIZE - 1 characters) declared as char line [TEXTSIZE]; from the terminal with function fgets(), use the function call: fgets(stdin, TEXTSIZE, line) Remember, stdin is a FILE* corresponding to the terminal, and fgets() stores the ' ' char at the end of the string, before the '' terminator. fgetsQ returns NULL when it encounters EOF. Here is a sample user session. User input is highlighted with yellow. Notice how the source string gets truncated if it is longer than the destination buffer.

Explanation / Answer

Here is the code for you:

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define TEXTSIZE 20

void justifyLeft(char str[])
{
int count = 0;
while(isspace(str[count++]));
char dest[TEXTSIZE];
int j = 0;
for(int i = count-1; str[i] != '' && i < 20; i++)
dest[j++] = str[i];
dest[j-1] = ' ';
while(j < 20)
dest[j++] = ' ';
dest[19] = '';
printf("%s", dest);
}
void justifyRight(char str[])
{
int count = strlen(str)-1;
if(count > 20)
count = 20;
//while(isspace(str[count--]));
char dest[TEXTSIZE];
int j = 18;
for(int i = count-1; i >= 0; i--)
dest[j--] = str[i];
while(j >= 0)
dest[j--] = ' ';
dest[19] = '';
printf("%s", dest);
}
int main()
{
char line[TEXTSIZE];
while(!feof(stdin))
{
printf("Enter the string: ");
fgets(line, TEXTSIZE, stdin);
fflush(stdin);
if(strcmp(line, "quit ") == 0)
return 0;
printf("Left: [");
justifyLeft(line);
printf("]");
printf(" Right: [");
justifyRight(line);
printf("] ");
}
}