By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,541 Members | 1,109 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,541 IT Pros & Developers. It's quick & easy.

reversing characters in a word

P: 16
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
Jan 16 '07 #1
Share this Question
Share on Google+
4 Replies


Soujiro
P: 35
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:
Expand|Select|Wrap|Line Numbers
  1.     MainFunction()
  2.     {
  3.         loop while list is not empty
  4.         { 
  5.             String something = getFrom list
  6.             print( reverseFunc(something) );
  7.             nextValueinList();
  8.         } 
  9.     }
  10.  
  11.     String reverseFunc( String something )
  12.     {
  13.         String temporary = something;
  14.         String result="";
  15.         for( int i=temporary.size()-1; i>= 0; i-- )
  16.         {
  17.             result+=temporary.charAt( i );
  18.         }
  19.         return result;
  20.     }
  21.  
The function was not tested though but i guess you can make it run..
Jan 16 '07 #2

P: 16
I was trying to work on the code but i keep getting the error
cannot find symbol
symbol : method size()
location: class java.lang.String
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 :)
Jan 16 '07 #3

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

10K+
P: 13,264
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()
Jan 16 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.