reversing characters in a word

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hello12
    New Member
    • Oct 2006
    • 16

    reversing characters in a word

    Hello,
    I am new to java and i was having a hard time figuring out on how to do certain string manipulations. I was asked to read in a text file and reverse the words. So far, I have put all the words in the text file into an arraylist.
    For example...
    Hello World. ---> must be printed out as --> olleH .dlroW
    right now when i print my arraylist i am getting
    [ Hello, World.] I was thinking of somehow accessing each word from here and reversing it. But i am stuck at this point. ANy help would
    be appreciated.
    thanks
  • Soujiro
    New Member
    • Jan 2007
    • 35

    #2
    From there.. You can do it in many ways.. Because you are new Ill teach you a slow, inefficent but easy one.

    here is the pseudocode:
    Code:
        MainFunction()
        {
            loop while list is not empty
            { 
                String something = getFrom list
                print( reverseFunc(something) );
                nextValueinList();
            } 
        }
    
        String reverseFunc( String something )
        {
            String temporary = something;
            String result="";
            for( int i=temporary.size()-1; i>= 0; i-- )
            {
                result+=temporary.charAt( i );
            }
            return result;
        }
    The function was not tested though but i guess you can make it run..

    Comment

    • hello12
      New Member
      • Oct 2006
      • 16

      #3
      I was trying to work on the code but i keep getting the error
      cannot find symbol
      symbol : method size()
      location: class java.lang.Strin g
      for(int i = (temp.size()-1);i>=0;i--)
      I did import java.io and java.util in the beginning of the program.
      thanks in advance :)

      Comment

      • hello12
        New Member
        • Oct 2006
        • 16

        #4
        it worked!!!!! thankyou.
        i changed the code a little bit..
        for(int i = (temp.length() -1); i>=0;i--)

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by hello12
          it worked!!!!! thankyou.
          i changed the code a little bit..
          for(int i = (temp.length() -1); i>=0;i--)
          Size of arraylist is given by .size()
          Length of string is given by .length()

          Comment

          Working...