Coding Practice

Create a program that reads some words and print it all right justified

Reads some words and print it all right justified

Suppose you have some words and you want to right justify them, that is, align them to the right. Create a program that reads some words and print it alright justified, within the same order as they seem within the input. [5 Marks]

Input:
The first line of the input will contain an integer N (1 ≤ N ≤ 50) indicating the amount of following words. Each word consists of up to 50 letters (‘A’-‘Z’ or 'a'-'z') and will contain a minimum of one letter.

Output:
Print the words padded on the left with space characters in order that they're all an equivalent length because the longest word found therein text

Sample Output
3
Sample Input:
Bod
Tommy
Jim

Sample Output:
  Bod
Tommy
  Jim

Process returned 0 (0x0)   execution time : 14.700 s
Press any key to continue.
Source Code
#include <stdio.h>
 
struct name{
    char name[55];
 
}list[100];
 
int main()
{
    int i, N, num = 5, maxLength = 0, j;
 
    scanf("%d", &N);
 
    printf("Sample Input:\n");
    for(i = 0; i < N; i++)
    {
        scanf("%s", list[i].name);
 
        for(j = 0; list[i].name[j] != '\0'; j++);
 
        if(j > maxLength)
        {
            maxLength = j;
        }
    }
 
    printf("\nSample Output:\n");
    for(i = 0; i < N; i++)
    {
        printf("%*s\n", maxLength, list[i].name);
    }
}
Sample Output
4
Sample Input:
LONGEST
a
LONGER
SHORT
 
Sample Output:
LONGEST
      a
 LONGER
  SHORT
 
Process returned 0 (0x0)   execution time : 28.554 s
Press any key to continue.

No comments:

Post a Comment

Change Theme
X