943,698 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 8407
  • Java RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Mar 5th, 2009
0

Re: How do i write a word backwards?

^ That's a very good solution, I was just going to suggest going through the String in reverse order and printing the current character.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Mar 6th, 2009
0

Re: How do i write a word backwards?

Click to Expand / Collapse  Quote originally posted by verruckt24 ...
@curtissumpter: what do you think you are doing giving people ready made code, this forum boasts an attitude opposite of that. Tell the OP the logic mentioning the way to go about, or at the most the psuedocode, but not written code in any manner.

@peedi : Use a stack to reverse the word and I am sure it will be the most convenient approach. Just keep pushing all the letters till the end of the word and then pop them back one by one.
yup stacks are easiest for that
push it all in the stack and pop n print it
Reputation Points: 11
Solved Threads: 7
Junior Poster
notuserfriendly is offline Offline
102 posts
since Jan 2007
Mar 6th, 2009
0

Re: How do i write a word backwards?

Why use a stack? You could just loop through the string and print each char in reverse order.

But still think using the built in StringBuilder is better.

Java Syntax (Toggle Plain Text)
  1. for (int i=s.length()-1;i>=0;i--) {
  2. System.out.print(s.charAt(i));
  3. }
  4. System.out.println();

Basically you start at the last character of the string and loop through each character in the String printing in reverse.
Reputation Points: 12
Solved Threads: 2
Newbie Poster
chili5 is offline Offline
21 posts
since Dec 2008
Mar 6th, 2009
0

Re: How do i write a word backwards?

yup stacks are easiest for that
push it all in the stack and pop n print it
But chances are if a person doesn't know how to use str.toCharArray(), they are not going to be familiar with various data structures. If that's the case you could suggest a doubly linked list or whatever, but at the level of the original poster it would seem kind of absurd. However, I do take your point of the original poster (not writing full code) however previous to my post there were others that were almost completely full code, they were just buggy. I simply clarified what was prior.

-- LiveWire
Reputation Points: 10
Solved Threads: 0
Light Poster
curtissumpter is offline Offline
25 posts
since Nov 2008
Mar 7th, 2009
0

Re: How do i write a word backwards?

Quote ...
Why use a stack? You could just loop through the string and print each char in reverse order.
Because they seem logical, don't they, you just run through the string in a forward order as apart from a backward order and then just pop out elements and they are reversed. After all Stacks are made for such use.

Quote ...
But chances are if a person doesn't know how to use str.toCharArray(), they are not going to be familiar with various data structures.
Absolutely wrong. str.toCharArray() is langauge specific thing, whereas stacks arent, not to mention they are the most basic of the data structures that are taught to beginners. Also since they do not "belong" to any language the chances that a beginner knows them are quite good than he knowing something like str.toCharArray() which is a Java specific method.

Quote ...
If that's the case you could suggest a doubly linked list or whatever
Wrong again. I haven't heard doubly link lists being use for string reversal anytime.
Reputation Points: 485
Solved Threads: 89
Posting Shark
verruckt24 is offline Offline
944 posts
since Nov 2008
Mar 7th, 2009
0

Re: How do i write a word backwards?

We'll have to agree to disagree. You are getting into teaching methods and styles saying that a data structure would be taught before a basic object. I know I wasn't taught data structures until my third course in computer science where as classes and methods (including built in methods) were taught in courses one and two. (Beginning programming and Advanced Programming). But maybe you were taught differently.

And almost any data structure could be used to reverse a string. A variety of trees. A doubly linked list. A stack. So to say "absolutely" when it is not absolute is absolutely wrong.
Reputation Points: 10
Solved Threads: 0
Light Poster
curtissumpter is offline Offline
25 posts
since Nov 2008
Mar 7th, 2009
0

Re: How do i write a word backwards?

We'll have to agree to disagree. You are getting into teaching methods and styles saying that a data structure would be taught before a basic object. I know I wasn't taught data structures until my third course in computer science where as classes and methods (including built in methods) were taught in courses one and two. (Beginning programming and Advanced Programming). But maybe you were taught differently.

And almost any data structure could be used to reverse a string. A variety of trees. A doubly linked list. A stack. So to say "absolutely" when it is not absolute is absolutely wrong.
Firstly, I didn't ever say that you are not taught classes and methods before data structures, I said, that one would certainly not be taught the str.toCharArray method before data structures if you understand that. Knowing specifics reqiures deeper understanding than generics thats the simple point here. Not to mention I am sure no class teaches the String class' methods in Java so a beginner is certainly not supposed to know that.

Also I never said that doubly linked lists cannot be used to reverse strings, you can write a program that uses that damn structure to reverse a string, there can be thousands of different implementations for a single thing and many of them can be absolutely unfitting in the sense that they won't solve the problem in the most optimized, logical way, I said that I haven't heard of they being used for such purpose. You can, for that matter, carry out a poll asking people what is a better DS to be used for string reversal ? Stack/Doubly linked list and then find out that 99.99% of them (all the sane people) would go for a stack.
Reputation Points: 485
Solved Threads: 89
Posting Shark
verruckt24 is offline Offline
944 posts
since Nov 2008
Mar 9th, 2009
0

Re: How do i write a word backwards?

I have to disagree with you Curtis. A "Data Structures" course typically assumes that you already have a knowledge of things like stacks and queues, which are generally taught in lower level courses. Not to rehash this argument. And in my opinion, any solution to the problem is helpful, as long as you explain the advantages and disadvantages of each. So there's nothing wrong with telling him how to do it with a doubly linked list as long as he's aware that a stack is probably the more logical way to do it.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Mar 9th, 2009
0

Re: How do i write a word backwards?

Okay, breathe guys. Breathe.

Just let it go.

-- Curtis
Reputation Points: 10
Solved Threads: 0
Light Poster
curtissumpter is offline Offline
25 posts
since Nov 2008
Mar 10th, 2009
0

Re: How do i write a word backwards?

Click to Expand / Collapse  Quote originally posted by peedi ...
I want the user to input a word, example happy
and i want the program to output on the screen, yppah
println break the line for output instead use print and display only the array...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rameshrajamani is offline Offline
2 posts
since Dec 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: switch off the client monitor display
Next Thread in Java Forum Timeline: How to count paragraph for an article?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC