Solved

Test if sequence of characters matches

Posted on 2007-11-15
4
176 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Spring Framework HTTPSession management 1 23
how to add new optional parameter to JSP 1 27
table example 4 21
configure dependency in POM for new database 3 15
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

912 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now