Solved

Test if sequence of characters matches

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

747 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

10 Experts available now in Live!

Get 1:1 Help Now