Program opened below. You can scroll through the algorithm, Java code and output.
Program 11
Vowels Arranged in a Square Matrix
Question
Design class ST to extract vowels from an input string. If their count is odd, add one vowel to make it even; arrange the vowels in a matrix with two columns and count/2 rows (a clarification of the ambiguous matrix order in the assignment).
Algorithm
Step 1: Start and create an object of class ST.
Step 2: Accept the input string.
Step 3: Initialize an empty string for extracted vowels.
Step 4: Inspect the input string character by character.
Step 5: Convert each inspected character to uppercase for comparison.
Step 6: Check whether it belongs to A, E, I, O or U.
Step 7: Append each vowel found, preserving its original form.
Step 8: Count the vowels by measuring the extracted string.
Step 9: If that count is odd, append the padding vowel A.
Step 10: If no vowel exists, display an appropriate message and stop.
Step 11: Calculate the number of rows as half the even vowel count.
Step 12: Create a two-column character matrix.
Step 13: Start the insertion position at zero.
Step 14: Fill cells in row-major order from the extracted vowels.
Step 15: Print each row and then stop.
Java program
Javaimport java.util.*;
class ST {
String text;
String vowels;
void input() {
Scanner sc = new Scanner(System.in);
text = sc.nextLine();
}
void arrange() {
vowels = "";
for (int i = 0; i < text.length(); i++) {
char ch = text.charAt(i);
char up = Character.toUpperCase(ch);
if ("AEIOU".indexOf(up) >= 0) {
vowels += ch;
}
}
if (vowels.length() % 2 != 0) {
vowels += 'A';
}
int rows = vowels.length() / 2;
if (rows == 0) {
System.out.println("No vowels");
return;
}
char[][] matrix = new char[rows][2];
int index = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < 2; j++) {
matrix[i][j] = vowels.charAt(index++);
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}
public static void main(String[] args) {
ST ob = new ST();
ob.input();
ob.arrange();
}
}Sample output
Input: AEIOU
Output: A E / I O / U A