Instantly share code, notes, and snippets.

@bytecodeman /Palindrome.java Secret
Last active Oct 12, 2018

Embed
What would you like to do?
CSC-220 In class Stack Exercise
import stack.*;
public class Palindrome {
public static void main(String[] args) {
// should return true
System.out.println(checkForPalindrome("abccba"));
// should return true
System.out.println(checkForPalindrome("Was it a car or a cat I saw?"));
// should return true
System.out.println(checkForPalindrome("I did, did I?"));
// should return false
System.out.println(checkForPalindrome("hello"));
// should return true
System.out.println(checkForPalindrome("Don't nod"));
}
public static boolean checkForPalindrome(String str) {
str = str.toLowerCase();
Stack<Character> charStack = new Stack<Character>();
String strForward = "";
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (ch >= 'a' && ch <= 'z') {
strForward += ch;
charStack.push(ch);
}
}
String strBackward = "";
while (!charStack.isEmpty())
strBackward += charStack.pop();
return strForward.equals(strBackward);
}
}
import stack.*;
public class Palindrome2 {
public static void main(String[] args) {
// should return true
System.out.println(checkForPalindrome("abccba"));
// should return true
System.out.println(checkForPalindrome("Was it a car or a cat I saw?"));
// should return true
System.out.println(checkForPalindrome("I did, did I?"));
// should return false
System.out.println(checkForPalindrome("hello"));
// should return true
System.out.println(checkForPalindrome("Don't nod"));
}
public static boolean checkForPalindrome(String str) {
str = str.toLowerCase();
Stack<Character> charStack = new Stack<Character>();
String strForward = "";
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (ch >= 'a' && ch <= 'z') {
strForward += ch;
charStack.push(ch);
}
}
int i = 0;
while (!charStack.isEmpty()) {
char stackChar = charStack.pop();
if (strForward.charAt(i) != stackChar)
return false;
i++;
}
return true;
}
}
public class PalindromeStarterCode {
public static void main(String[] args) {
// should return true
System.out.println(checkForPalindrome("abccba"));
// should return true
System.out.println(checkForPalindrome("Was it a car or a cat I saw?"));
// should return true
System.out.println(checkForPalindrome("I did, did I?"));
// should return false
System.out.println(checkForPalindrome("hello"));
// should return true
System.out.println(checkForPalindrome("Don't nod"));
}
public static boolean checkForPalindrome(String string) {
return false;
}
}
// Generic Code Derived from Example StackOfIntegers2b.java
package stack;
import java.util.*;
public class Stack<T> {
private ArrayList<T> list;
public Stack() {
list = new ArrayList<T>(16);
}
public T push(T item) {
list.add(item);
return item;
}
public T pop() {
if (getSize() <= 0)
throw new RuntimeException("Can't pop empty stack.");
T item = list.get(getSize() - 1);
list.remove(getSize() - 1);
return item;
}
public T peek() {
if (getSize() <= 0)
throw new RuntimeException("Can't peek empty stack.");
return list.get(getSize() - 1);
}
public boolean isEmpty() {
return list.isEmpty();
}
public int getSize() {
return list.size();
}
public int search(int i) {
return list.lastIndexOf(i);
}
/** Override the toString in the Object class */
public String toString() {
return "stack: " + list.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment