C PROGRAMMING QUESTION Problem 3 Justified Write a function

C PROGRAMMING QUESTION

Problem 3. Justified Write a function called justityLeft0 that justifies a string left and a function called justityRight0 that justifies a string right. The function signatures are given next: int justify Left const charl] src, char dest0, int maxsize); int justifyRight (const charl] src, char destD, 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 \"W0\' terminator. Consider that it is 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.

Solution

Code:

#include<stdio.h>
int justifyLeft(char src[], int max_size);
int justifyRight(char src[], int max_size);
int main (){
char src[100];
while (1){
scanf(\"%s\", src);
if (!strcmp(src,\"quit\")){
break;
}else{
justifyLeft(src,10);
justifyRight(src,10);
printf(\"\ \");
}
  
}

  
  
return 0;
}
int justifyLeft(char src[], int max_size){
int i,s_len;
char *dest;
dest = malloc(max_size);
s_len = strlen(src);
strcat(dest,src);
char *str1[max_size-s_len];
for(i=0;i<(max_size-s_len);i++){
strcat(dest,\" \");
}
printf(\"Left: [%s]\",dest);
  
}
int justifyRight(char src[], int max_size){
int i,s_len;
char *dest;
dest = malloc(max_size);
s_len = strlen(src);
char *str1[max_size-s_len];
for(i=0;i<(max_size-s_len);i++){
strcat(dest,\" \");
}
strcat(dest,src);
printf(\" Right : [%s]\",dest);
  
}

Output:

C PROGRAMMING QUESTION Problem 3. Justified Write a function called justityLeft0 that justifies a string left and a function called justityRight0 that justifies
C PROGRAMMING QUESTION Problem 3. Justified Write a function called justityLeft0 that justifies a string left and a function called justityRight0 that justifies

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site