Solved

Test if sequence of characters matches

Posted on 2007-11-15
4
197 Views
Last Modified: 2013-11-23
Hi all

Looking for some help with trying to create a method that will read a sequence of letters from start to end as the end to start
ie "test i tset" or "I'm an ee na m'I" ignoring spaces or punctuation

What im trying to do is have one queue holiding the user input unformatted and then by value put in a stack then convert to a second queue in a formatted state ie "I'm an ee na m'I" in queue 1 would be "IMANEENAMI" then return if the text reads start to end and end to start?

I'll post my code im using for my queues and stacks in additional posts.

Any help would be greatly appreciated
Many Thanks
Steve
0
Comment
Question by:Stephen Manderson
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 19

Author Comment

by:Stephen Manderson
ID: 20295833
//Stack

public interface Stack
{
      public boolean stackEmpty();
      public boolean stackFull();
      public void pushStack(Object x);
      public Object popStack();
      public Object peekStack();


}
     
public class ContigStack implements Stack
{
      public static final int SIZE = 1000;
      private int max;
      private Object data[];
      private int head;
      private int tail;

      BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

      public void pushStack(Object x)
      {
            if (this.stackFull()) throw new RuntimeException("Stack Full");
            Node temp = new Node(x, head);
            Head = temp;
      }
     
      public Object popStack()
      {
          if (this.stackEmpty()) return null;
          Node temp = head;
          head = head.getNext();
          return temp.getContents();
      }

    public Object peekStack()
      {
       if (this.stackEmpty()) return null;
       return head.getContents();
      }
}

//Queue

public interface Queue  
 
{   public boolean queueEmpty();  
     public boolean queueFull();  
     public void queueAdd(Object x);  
     public Object queueTake();  
     public Object queueShow();  
}

==========================================

public abstract class ContigQ implements Queue // class must be abstract
 
{  public static final int SIZE = 1000;  
private int max;  
private Object data[];  
private int head;  
private int tail;  
}

==========================================

public class LinkQueue implements Queue  
{
      // I suppose you already have this class...
      private static class Node {
            private Object o;
            private Node next;

            Node(Object o, Node next) {
                  this.o = o;
                  this.next = next;
            }

            void setNext(Node next) { this.next = next; }

            Node getNext() { return next; }

            Object getContents() {
                  return o;
            }
      }

      private Node head;  
    private Node tail;  
 
public LinkQueue()  
{  head = null;  // head, you wrote heal...
    tail = head;  
}  
 
public boolean queueEmpty()  
{  return (head == null); // corrected...
}  
 
public boolean queueFull()  
{  return false;
}  

public void queueAdd(Object x) // corrected...  
{   if (queueFull()) {}
      // you don't need to use "this" key word
      // also since you are sure that queue can't be full
      // you don't need to check this.
else  
{  Node temp = new Node(x, null);  
    if (head == null)  
            head = temp;  
    else tail.setNext(temp);  
    tail = temp;  
    }  
}  
 
public Object queueShow() {
      if (queueEmpty()) {
            return null;
      }
      else return head.getContents();  
}  
 
public Object queueTake()  
{  if (queueEmpty()) {
      return null;
      // or you can throw an Exception if you want.
   }
else  
{  Node temp = head;  
    head = head.getNext();  
         if (head == null) tail = null;  
    return temp.getContents();  
    }  
}

      // added method - String representation...
      public String toString() {
            StringBuffer s = new StringBuffer();
            Node n = head;
            while (n != null) {
                  s.append(n.getContents());
                  if (n.getNext() != null)
                        s.append(',');
                  n = n.getNext();
            }
            return s.toString();
      }
}

=========================================

public class Main {
      public static void main(String[] args) {
            LinkQueue queue = new LinkQueue();

            queue.queueAdd(new Integer(1));
            queue.queueAdd(new Integer(2));
            queue.queueAdd(new Integer(3));
            queue.queueAdd(new Integer(4));
            queue.queueAdd(new Integer(5));
           
            queue.queueTake();
            queue.queueTake();

            System.out.println(queue);
     
            // 3,4,5
      }
}
0
 
LVL 19

Author Comment

by:Stephen Manderson
ID: 20296018
And lastly I have the following class which allows user input via keyboard and have a compare character class already in place



public class CharContainer {

      private char value;

      public CharContainer() {
            value = ' ';
      }

      public CharContainer(char newValue) {
            value = newValue;
      }

      public void setValue(char newValue) {
            value = newValue;
      }

      public void setValueFromKeyboard() {
            try {
                  BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
                  System.out.print("Enter a character to store in the class: ");
                  value = (char) in.read();
                  System.out.println();

            }
            catch (Exception e) {
            }

      }

      public char getValue() {
            return value;
      }

      public void printToScreen() {
            System.out.println(value);
      }

      public void printToFile(String filename) {
            try {
                   PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(filename)));
                   out.println(value);
                   out.close();
            }
            catch (Exception e) {
            }
      }


      public boolean eq(CharContainer other) {
            if (value == other.getValue()) {
                  return true;
            } else {
                  return false;
            }
      }

      public boolean gt(CharContainer other) {
            if (value > other.getValue()) {
                  return true;
            } else {
                  return false;
            }
      }

      public boolean ge(CharContainer other) {
            if (value >= other.getValue()) {
                  return true;
            } else {
                  return false;
            }
      }
}


0
 
LVL 13

Expert Comment

by:Bart Cremers
ID: 20296729
What's the purpose of this exercise? Is it the string manipulation, or is it writing your own stack/queue?

Anyways, I can't get your code to compile. Missing Node and Head classes.

0
 
LVL 11

Accepted Solution

by:
spoxox earned 500 total points
ID: 20332066
After 5 days without response, perhaps this is a dead issue. But just in case... Same question as Bart_Cr: what's the purpose? I'm having trouble understanding the issue.

If the whole point is to read a string, strip non-alphanumeric characters, and test to see if it's a palindrome (reads the same starting at either end, e.g. "A man, a plan, a canal: Panama."), then one solution is: get the user input, strip unwanted characters, and test recursively like this:

boolean palindromic(String aString) {
  if (aString.length() < 2)  // base case

    return true;

  else  

  // recursive case: if terminating characters are equal,  
    return ( aString.charAt(0) == aString.charAt(aString.length() - 1) )
  // and remaining substring is a palindrome, this string is a palindrome.
    &&  palindromic(aString.substring(1, aString.length() - 1));
}

Stripping out unwanted characters might be done by checking the input one character at a time and appending the characters you like (maybe isLetter(input.charAt(i))) to a new StringBuffer.


If the whole point is to use homegrown data structures, then this will not have been helpful! If queues and stacks are mandated, I guess I would take the input character by character, discarding those I didn't like, and put a copy of each character into both a queue (FIFO) and a stack (LIFO). When done adding input, I'd pop both until one was empty (queue.isEmpty() || stack.isEmpty()) or until the characters were unequal (queue.pop() != stack.pop(), more or less). Feels like that should work.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question