import java.util.*;
public class qstn_3
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of words");
int n = sc.nextInt();
//To shift the command to next line as after an integer input when we take a string
// input the command does not shift to next line and is rather skipped.
//It does not read the newline character in your input created by hitting "Enter,"
sc.nextLine();
//Boundary cases
if(n<2 && n>9)
{
System.out.println("Invalid Input");
System.exit(0);
}
// word array
String s[] = new String[n];
// length array
int length[] = new int[n];
for(int i = 0;i<n;i++)
{
// taking the string input
s[i] = sc.nextLine();
// length of each word stored in the array
length[i] = s[i].length();
}
int j = 0;
String k = "";
int flag = 0;
// infinite loop as we don't know the length of
// maximum word.
while(true)
{
// Running the loop for each word
for(int i = 0;i<n;i++)
{
// This will check if the length is not zero
if(length[i]!=0)
{
// We extract the character
char c = s[i].charAt(j);
//We store the character in the variable with a space
k = k+c+" ";
// We decrease the length
length[i]--;
// A check variable for exit condition
flag++;
}
// If there are no character, we just add two spaces.
else
{
k = k+" ";
}
}
// We increase the character position
j++;
// Display of the words
System.out.println(k);
// Erasing the word for next loop
k="";
//Exit condition when no more character in any word
if(flag==0)
{
System.exit(0);
}
//Initializing the flag to zero for next loop
flag = 0;
}
}
}
No comments:
Post a Comment