1 | |
2 | |
3 | import java.util.Scanner; |
4 | public class Activity_5_ITC_101 { |
5 | |
6 | public static void main(String[] args) { |
7 | Scanner sc = new Scanner(System.in); |
8 | |
9 | while (true) { |
10 | |
11 | mainFrame(sc); |
12 | } |
13 | |
14 | } |
15 | |
16 | public static void mainFrame (Scanner sc) { |
17 | System.out.println("\tACTIVITY 5 ITC 101\n"); |
18 | System.out.println("1. String length and character Extraction"); |
19 | System.out.println("2. Convert and Combine Strings"); |
20 | System.out.println("3. Extract Substring"); |
21 | System.out.println("4. Revrse String"); |
22 | System.out.println("5. Find and count a Character"); |
23 | |
24 | int selection = sc.nextInt(); |
25 | sc.nextLine(); |
26 | |
27 | selection(selection,sc); |
28 | } |
29 | |
30 | |
31 | public static void selection (int selection, Scanner sc) { |
32 | switch (selection) { |
33 | case 1: |
34 | act1(sc); |
35 | break; |
36 | |
37 | case 2: |
38 | act2(sc); |
39 | break; |
40 | |
41 | case 3: |
42 | act3(sc); |
43 | break; |
44 | |
45 | case 4: |
46 | act4(sc); |
47 | break; |
48 | |
49 | case 5: |
50 | act5(sc); |
51 | |
52 | break; |
53 | |
54 | default: |
55 | System.out.println("You Entered an invalid Selection"); |
56 | break; |
57 | } |
58 | } |
59 | |
60 | |
61 | public static void act1 (Scanner sc) { |
62 | |
63 | |
64 | System.out.print("Enter Word: "); |
65 | String word = sc.nextLine(); |
66 | System.out.println("Length of Word: " + wordlenght(word)); |
67 | System.out.println("First Letter: " + word.charAt(0)); |
68 | System.out.println("Last Letter: " + word.charAt(word.length() -1)); |
69 | } |
70 | |
71 | public static int wordlenght (String word) { |
72 | int wordnum = word.length(); |
73 | |
74 | return wordnum; |
75 | } |
76 | |
77 | |
78 | public static void act2 (Scanner sc) { |
79 | System.out.print("Enter the First Word: "); |
80 | String word1 = sc.nextLine(); |
81 | |
82 | System.out.print("Enter Second Word: "); |
83 | String word2 = sc.nextLine(); |
84 | |
85 | System.out.println(convertCombine(word1, word2)); |
86 | } |
87 | |
88 | public static String convertCombine (String word1 , String word2) { |
89 | word1 = word1.toUpperCase(); |
90 | word2 = word2.toLowerCase(); |
91 | String combined = word1.concat(word2); |
92 | |
93 | return "Combined String is: "+ combined; |
94 | } |
95 | |
96 | |
97 | public static void act3 (Scanner sc) { |
98 | System.out.println("Enter a Sentence: "); |
99 | String word = sc.nextLine(); |
100 | |
101 | System.out.println(extracting(word)); |
102 | } |
103 | |
104 | public static String extracting (String word) { |
105 | |
106 | if(word.length() < 5) { |
107 | return "First 5 Characters: " + word; |
108 | } else { |
109 | return word.substring(0,5); |
110 | } |
111 | } |
112 | |
113 | |
114 | public static void act4 (Scanner sc) { |
115 | System.out.print("Enter a Word: " ); |
116 | String word = sc.nextLine(); |
117 | |
118 | System.out.println("Reversed: " + reverse(word)); |
119 | |
120 | } |
121 | |
122 | public static String reverse (String word) { |
123 | String newWord= ""; |
124 | char ch; |
125 | |
126 | for(int i = word.length() -1; i >= 0; i--) { |
127 | ch = word.charAt(i); |
128 | newWord += ch; |
129 | } |
130 | |
131 | return newWord; |
132 | } |
133 | |
134 | |
135 | public static void act5 (Scanner sc) { |
136 | System.out.print("Enter a Sentence: "); |
137 | String word = sc.nextLine(); |
138 | |
139 | System.out.print("Enter Character: "); |
140 | char ch = sc.next().charAt(0); |
141 | |
142 | |
143 | System.out.println("The Character " + ch + " Appears " + numberOfchar(word, ch) + " Times in the Sentence"); |
144 | } |
145 | |
146 | public static int numberOfchar (String word, char ch) { |
147 | |
148 | int count = 0; |
149 | |
150 | for(int i = 0 ; i < word.length(); i++) { |
151 | if(ch == word.charAt(i)) { |
152 | |
153 | count++; |
154 | } |
155 | } |
156 | |
157 | return count; |
158 | } |
159 | |
160 | |
161 | |
162 | |
163 | } |