Write only in C code, not C++ please. Problem 3 : Justified! Write a function ca
ID: 3574951 • Letter: W
Question
Write only in C code, not C++ please.
Problem 3 : Justified!
Write a function called justifyLeft0 that justifies a string left and a function called justityRight0that justifies a string right. The function signatures are given next: int justifyLeft (const char src[], char dest[], int maxsize); int justifyRight (const char src[], char dest[], int maxsize); String src is the source string, string dest in the destination buffer that holds the result of the opration, 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 stirng. 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 src may have more no-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. Wright 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 justifyRightO 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(line, TEXTSIZE, stdin). 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("] ");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.