MohtasaUnique
Grand Master
- Joined
- Sep 1, 2007
- Sythe Gold
- 690
- Messages
- 6,681
Below I have the program as I've made it so far. Now I have to change it to check if vectors are palindromatic instead of a string input. For example:
First one is a palindrome, whereas the second isn't.
It HAS to use stacks and queues. I honestly have no idea what the fuck vectors are or how to use them in programming. So halp plz :c
First one is a palindrome, whereas the second isn't.
It HAS to use stacks and queues. I honestly have no idea what the fuck vectors are or how to use them in programming. So halp plz :c
PHP:
package project4;
// Import
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
public class Project4
{
public static void main(String[] args) throws IOException
{
// Open and read input file
String fileName = "input.txt";
BufferedReader bufferedReader = new BufferedReader(new FileReader(fileName));
String line;
while((line = bufferedReader.readLine()) != null)
{
// Return appropriate evaluation of the string
System.out.print("Reading from file...\n");
if (is_palindrome(line))// If true, it's a palindrome
System.out.println("That is a palindrome.");
else // otherwise, it's not
System.out.println("That is not a palindrome.");
}
bufferedReader.close();
}
public static boolean is_palindrome(String input)
{
// Make a new queue, stack, and character object
Queue<Character> q = new LinkedList<>();
Stack<Character> s = new Stack<>();
Character letter;
int mismatches = 0;
int i;
for (i = 0; i < input.length(); i++)
{
letter = input.charAt(i);
if (Character.isLetter(letter))
{
q.add(letter);
s.push(letter);
}
}
while (!q.isEmpty())
{
if (q.remove() != s.pop())
mismatches++; // Increment "mismatches" if q != s
}
return (mismatches == 0); // will return true if the method did not change the variable "mismatches"
}
}

