How do i write a word backwards?

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2008
Posts: 1,619
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 205
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: How do i write a word backwards?

 
0
  #11
Mar 5th, 2009
^ That's a very good solution, I was just going to suggest going through the String in reverse order and printing the current character.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 74
Reputation: notuserfriendly is an unknown quantity at this point 
Solved Threads: 5
notuserfriendly notuserfriendly is offline Offline
Junior Poster in Training

Re: How do i write a word backwards?

 
0
  #12
Mar 6th, 2009
Originally Posted by verruckt24 View Post
@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
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 21
Reputation: chili5 is an unknown quantity at this point 
Solved Threads: 2
chili5's Avatar
chili5 chili5 is offline Offline
Newbie Poster

Re: How do i write a word backwards?

 
0
  #13
Mar 6th, 2009
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.

  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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 25
Reputation: curtissumpter is an unknown quantity at this point 
Solved Threads: 0
curtissumpter curtissumpter is offline Offline
Light Poster

Re: How do i write a word backwards?

 
0
  #14
Mar 6th, 2009
Originally Posted by notuserfriendly View Post
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 823
Reputation: verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough 
Solved Threads: 73
verruckt24's Avatar
verruckt24 verruckt24 is offline Offline
Practically a Posting Shark

Re: How do i write a word backwards?

 
0
  #15
Mar 7th, 2009
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.

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.

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.
Get up every morning and take a look at the Forbes' list of richest people. If your name doesn't appear.... GET TO WORK !!!
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 25
Reputation: curtissumpter is an unknown quantity at this point 
Solved Threads: 0
curtissumpter curtissumpter is offline Offline
Light Poster

Re: How do i write a word backwards?

 
0
  #16
Mar 7th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 823
Reputation: verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough 
Solved Threads: 73
verruckt24's Avatar
verruckt24 verruckt24 is offline Offline
Practically a Posting Shark

Re: How do i write a word backwards?

 
0
  #17
Mar 7th, 2009
Originally Posted by curtissumpter View Post
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.
Get up every morning and take a look at the Forbes' list of richest people. If your name doesn't appear.... GET TO WORK !!!
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,619
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 205
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: How do i write a word backwards?

 
0
  #18
Mar 9th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 25
Reputation: curtissumpter is an unknown quantity at this point 
Solved Threads: 0
curtissumpter curtissumpter is offline Offline
Light Poster

Re: How do i write a word backwards?

 
0
  #19
Mar 9th, 2009
Okay, breathe guys. Breathe.

Just let it go.

-- Curtis
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 2
Reputation: rameshrajamani is an unknown quantity at this point 
Solved Threads: 0
rameshrajamani rameshrajamani is offline Offline
Newbie Poster

Re: How do i write a word backwards?

 
0
  #20
Mar 10th, 2009
Originally Posted by peedi View Post
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...
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC