Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
69 lines (55 loc) · 1.86 KB
/
Copy pathMain.java
File metadata and controls
69 lines (55 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
StringAnalyzer s1 = new StringAnalyzer();
System.out.println("Enter string to reverse: ");
String order = sc.nextLine();
s1.input(order);
s1.printReverse();
System.out.println("\nVowels : " + s1.countVowels());
System.out.println("Palindrome: " + s1.isPalindrome());
s1.buildStudentReport(new String[] { "Hanan", "Qaisar", "Ahtisham" }, new double[] { 67, 65, 45 });
sc.close();
}
}
class StringAnalyzer {
String[] word;
public void input(String order) {
word = order.trim().split(" ");// split the order string by spaces amd then store them in word String
}
public void printReverse() {
int i = word.length;
for (int j = i - 1; j >= 0; j--) {
System.out.print(word[j] + " ");
}
}
public int countVowels() {
int count = 0;
for (String w : word) {
for (char ch : w.toLowerCase().toCharArray()) {
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
++count;
}
}
}
return count;
}
public boolean isPalindrome() {
int j = word.length;
for (int i = 0; i < word.length; i++) {
if (word[i].equalsIgnoreCase(word[j - 1])) {// (j-1)bcz indexing start from 0 and end at n-1
j--;
} else {
return false;
}
}
return true;
}
public void buildStudentReport(String[] names, double[] scores) {
System.out.println("Student Report: ");
for (int i = 0; i < names.length; i++) {
System.out.println((i + 1) + ". " + names[i] + " --- " + scores[i]);
}
}
}