@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
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.
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.
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.
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.
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.
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.