Link to home
Start Free TrialLog in
Avatar of Stephen Manderson
Stephen MandersonFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Test if sequence of characters matches

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
Avatar of Stephen Manderson
Stephen Manderson
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

//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
      }
}
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;
            }
      }
}


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.

ASKER CERTIFIED SOLUTION
Avatar of spoxox
spoxox
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial