-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion64_MiddleOne.java
More file actions
54 lines (41 loc) · 1.29 KB
/
Question64_MiddleOne.java
File metadata and controls
54 lines (41 loc) · 1.29 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
package week6_String;
import java.util.Scanner;
public class Question64_MiddleOne {
public static void main(String[] args) {
// olcay // Jul 6, 2020
/*You have a word, do the following:
1. When word has odd number of characters and:
- 3 or more characters, print middle letter
oak ==> a
javav ==> v
- Single character, print that character 3 times
# ==> ###
q ==> qqq
2. When word has even number of characters and:
- 4 or more characters, print middle 2
java ==> av
apples ==> pl
#$%^&* ==> %^
- 2 characters, print those 2 characters twice
@@ ==>@@@@
$$ ==>$$$$
hi ==> hihi
*/
Scanner scan = new Scanner(System.in);
System.out.println("Write a word:");
String word = scan.next();
if(word.length()%2!=0) {
if(word.length()==1) {
System.out.println(word+word+word);
}else if(word.length()>=3) {
System.out.println(word.charAt(word.length()/2));
}
}else {
if(word.length()==2) {
System.out.println(word+word);
}else if(word.length()>=4){
System.out.println(word.substring(word.length()/2-1, word.length()/2+1));
}
}
}
}