The Artima Developer Community
Sponsored Link

Java Answers Forum
palindrom function

1 reply on 1 page. Most recent reply: Apr 23, 2003 11:55 PM by jake

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
jake

Posts: 83
Nickname: onorok
Registered: May, 2002

palindrom function Posted: Apr 23, 2003 11:08 PM
Reply to this message Reply
Advertisement
Return true if the string is a palindrom, I have to use a queue and a stck to do this, "qu" is the queue and "st" is the stack. does anyone see whats wrong with this. A palindrom is a word that reads the same front and back.

public boolean isPal(String s)
	{
		stack st=new stack();
		Queue qu=new Queue();
		String x;
		
		for(int i=0; i<s.length(); ++i)
		{
			x = String.valueOf(s.charAt(i));
			qu.enqueue(x);//pushes it on the top of the queue
			
		}
		
		for(int v=s.length()-1; v>=0 ; --v)
		{	
			x = String.valueOf(s.charAt(v));
			if(x==qu.peek())//peek just returns the top item.
			{
				System.out.println("I got here");
				st.push(qu.dequeue());//dequeue pops it off the queue and returns it.
			}
			
		}
		return true;
	}


jake

Posts: 83
Nickname: onorok
Registered: May, 2002

Re: palindrom function Posted: Apr 23, 2003 11:55 PM
Reply to this message Reply
fixed the problem, it works now.

Flat View: This topic has 1 reply on 1 page
Topic: JDBC IN WINDOWS 98 Previous Topic   Next Topic Topic: Whats wrong with this code

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use