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

write a c program that uses the function prototypes int justifyLeft(const char[]

ID: 3776983 • Letter: W

Question

write a c program that uses the function prototypes

int justifyLeft(const char[] src, char dest[], int maxsize);

which justifies a string left

int justifyRight(const char[] src, char dest[], int maxsize);

which justifies a string right

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

*ensure that possible that src 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.

*the main function()  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 when the user enters the word “quit”.

*To read one line of text (with max. TEXTSIZE - 1 characters) declared as char line[TEXTSIZE];

and from the terminal with function fgets(), use the function call: fgets(stdin, TEXTSIZE, line)

output should be:

peace

Left: [peace ] Right [ peace] and equal rights

Left: [and equal rights ] Right [ and equal rights] harmony

Left: [harmony ] Right [ harmony] the right of the people peaceably to assemble

Left: [the right of the pe] Right [the right of the pe]

Explanation / Answer

#include<stdio.h>
#include <string.h>
#define MAX 20
//Left justified
int justifyLeft(const char src[], char dest[], int maxsize)
{
    int c;
    //copies source to destination
    for(c = 0; c < src[c] != ''; c++)
         dest[c] = src[c];
    dest[c] = ' ';
    dest[c+1] = '';
    //Prints left justfied
    printf("Left[%s]", dest);
    return 1;
}
//Right Justified
int justifyRight(const char src[], char dest[], int maxsize)
{
    int c;
    //Copies source to destination
    dest[0] = ' ';
    for(c = 0; c < src[c] != ''; c++)
         dest[c+1] = src[c];
    dest[c] = '';
    printf(" Right[%s]", dest);
    printf(" ");
    return 1;
}
int main()
{
    int c;
    char src[MAX], des[MAX];
    fgets(src, BUFSIZ, stdin);
    //Accept string till quit is entered
    do
    {
        for(c = 0; c < src[c] != ''; c++) ;
        justifyLeft(src, des, c);
        justifyRight(src, des, c);
        fgets(src, BUFSIZ, stdin);
    }while (strcmp(src, "quit ") != 0);
}


Output:

this
Left[this ] Right[ this]
this is demo
Left[this is demo ] Right[ this is dem]
this is test
Left[this is test ] Right[ this is tes]
check it
Left[check it ] Right[ check it]
quit