-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPalindrome.java
More file actions
78 lines (74 loc) · 3.35 KB
/
Palindrome.java
File metadata and controls
78 lines (74 loc) · 3.35 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
70
71
72
73
74
75
76
77
78
//Name: Rishi S.
//Date: 1/4/21
//Program Name: Palindrome.java
import java.util.Scanner;
public class Palindrome {
Scanner s1 = new Scanner(System.in);
public static void main(String[] args) {
Palindrome p1 = new Palindrome();
p1.runner();
}
public void runner() {
String finalStr = "";
System.out.print("\n\n\nWould you like to run Palindrome(1) or ReverseIt(2)? ");
int choice = s1.nextInt();
if(choice == 1) {
System.out.println("\nPlease enter a String:");
String str = s1.nextLine();
String newStr = str.replace(" ", "");
System.out.println("Size of Palindrome: ");
int size = s1.nextInt();
for(int x = 0; x<= newStr.length()-size; x++) {
if(!newStr.substring(x , x + size).contains(",")) {
boolean result = reverse(newStr.substring(x , x + size));
if(result == true) {
finalStr = finalStr + newStr.substring(x , x + size) + ", ";
}
}
}
String finalAnswer = finalStr.substring(0, finalStr.length()-1);
System.out.println("Here are your palindromes from your list:");
System.out.println(finalAnswer);
System.out.print("\n\n\n");
} else if(choice == 2) {
String finalString = "";
String str = "";
String newStr = "";
System.out.println("Please enter your string(s) followed by an enter:");
str = s1.next();
if(str.equals("#Done#")) {
System.out.println("Please enter at least one string other than #Done#.");
} else {
finalString = "_" + str + finalString;
}
while(!str.equals("#Done#")) {
str = s1.nextLine();
if (!str.equals("#Done#")) {
newStr = reverse2(str);
finalString = "_" + newStr + finalString;
}
}
if(!finalString.equals("")) {
System.out.println("Here is your word reversed: ");
System.out.println(finalString.substring(1, finalString.length()-1));
}
}
}
public String reverse2(String str) {
String reversedStr = "";
for(int x = str.length()-1; x>=0; x--) {
reversedStr = reversedStr + str.charAt(x);
}
return reversedStr;
}
public boolean reverse(String str) {
String reversedStr = "";
for(int x = str.length()-1; x>=0; x--) {
reversedStr = reversedStr + str.charAt(x);
}
if(reversedStr.equals(str)) {
return true;
}
return false;
}
}