java Strings

topic posted Tue, October 5, 2004 - 1:52 PM by  Jerome
Share/Save/Bookmark
Advertisement
Hello,



· Write a java program that reads in an input string (provided by user) and prints out the string in reverse order.
· Any numbers (0-9) should be spelled out. (see Examples)

Important: You cannot use any built-in functions that automatically reverse the string for you.

********************************************
Example1:

If I were to type:

java ReverseString abc123

Then the result would be:

Three Two One cba
********************************************
Example2:

If I were to type:

java ReverseString 123abc

Then the result would be:

cba Three Two One
********************************************

Please note that if no string is passed, the program should exit gracefully (not by exception) with a message to the user regarding the usage of the program.



posted by:
Jerome
North Carolina
Advertisement
Advertisement
  • Unsu...
     

    Re: java Strings

    Tue, October 5, 2004 - 2:50 PM
    You're asking us to write the code?

    Is this your homework?
    • Re: java Strings

      Tue, October 5, 2004 - 3:29 PM
      No i am asking anyone to write the code for me all i want is for someone to point me in the right direction because i dont know where to start is all.So can someone plaese help me?
      • Unsu...
         

        Re: java Strings

        Tue, October 5, 2004 - 3:35 PM
        Have you dealt with arrays yet?

        Break the string into a character array.

        Reorder the array elements.

        If the string abc123, then a is element 0 and 3 is element 5.

        0 = 5
        1 = 4
        2 = 3
        3 = 2
        4 = 1
        5 = 0

        Except you want it to handle strings of any length, so you use a for() loop.



  • Unsu...
     

    Re: java Strings

    Tue, October 5, 2004 - 4:13 PM

    I've always found it helpful to forget about code for the first steps. With problems like these, the best place to start is to sit down with a pencil and paper and actually do it and take note of the steps you use to do it.
    For example:
    1) a random string of characters like, abc123
    2) Look at the last character in the string.
    2a) Is it a number? If so, print out the word representation
    2b) If not, print out the number
    3) Take the next-to-last character
    4) Repeat 2a and 2b

    Now for starters, you see that steps 2a and 2b are going to be repeated for each character in the string. That tells you that you'll need a loop of some sort. You're going to do an if/then on the character to see if it's a number.

    You haven't written a single line of code yet, but you've gotten a start on how to do it.


    • Re: java Strings

      Wed, October 6, 2004 - 7:54 AM
      This is exactly the way you should always approach your code assignment.

      Some methods you may find useful for this particular code:

      - String has a method getCharArray() returns a character array representation of the invoking String object.

      - Character class has a static method isLetter(char ch) which returns true if a ch is a letter and false otherwise.

Recent topics in "Java Monkeys"

Topic Author Replies Last Post
Javascripting costs Schirin 3 May 17, 2007
Design patterns and principles emblylan 6 February 14, 2007
open source hosting alternatives? Mark 2 February 3, 2007
Ruby > Java Unsubscribed 6 January 9, 2007