My String Reversing Program in Java

Java programming topics
Post Reply
User avatar
Nipuna
Moderator
Moderator
Posts: 2729
Joined: Mon Jan 04, 2010 8:02 pm
Location: Deraniyagala,SRI LANKA

My String Reversing Program in Java

Post by Nipuna » Mon Oct 22, 2012 5:30 pm

Hi

I am back into my Java studies and wrote a little program to test my memory and also for fun. :)

So here is the program, This takes a string and then reverse it.

Code: Select all

import java.util.Scanner;

public class StringReverse{
    
    public static void main(String args[]){
        
        Scanner input = new Scanner(System.in);
        
        System.out.println("Enter your String: ");
        
        String s = input.nextLine();
        
        char c[]= s.toCharArray();
        
        for(int i=c.length-1; i>=0; i--){
            System.out.print(c[i]);  
            
        }
        System.out.println();
    }
}
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: My String Reversing Program in Java

Post by Neo » Fri Oct 26, 2012 8:36 am

Very good. Nicely written with indents. It would be better if you can add comments for most important operations.
For example: Before the "for" loop, you would add a comment as "Loop runs in reverse direction to print each letter of the string".

Also, in the easy way, you can use a library function to reverse the string automatically.

Code: Select all

String revstr = new StringBuffer(s).reverse().toString();
System.out.println ("Reversed string: " + revstr);
However for learning, your code is very good.
User avatar
Nipuna
Moderator
Moderator
Posts: 2729
Joined: Mon Jan 04, 2010 8:02 pm
Location: Deraniyagala,SRI LANKA

Re: My String Reversing Program in Java

Post by Nipuna » Fri Oct 26, 2012 8:40 am

Thanks friend.

By the way, I always forget to add comments :) I must correct it quickly because it's not a good practice :)

And if you have time can you fix this "? ? ? ?" problem? It makes our codes ugly :)






Thanks
User avatar
Nipuna
Moderator
Moderator
Posts: 2729
Joined: Mon Jan 04, 2010 8:02 pm
Location: Deraniyagala,SRI LANKA

Re: My String Reversing Program in Java

Post by Nipuna » Fri Oct 26, 2012 8:56 am

Oh I forgot,

Thanks for teaching about that library I didn't know that before, I barely remember have heard of it a little but not sure.

I have so many to learn,
by the way I've also finished learning HTML/XHTML and CSS ( There is nothing called "finished", :) still learning but I've learned some so I can do something useful with them, (Actually finished both 2 tutorials in w3schools and I have knowledge now,Did some practices too :) ) Next JavaScript,XML and PHP)

Then will learn some more languages :) But Java is huge and have so many to learn. :o

Oh almost forget

I need your valuable advice and I was waiting so many days to ask this from you :)
After learning these basic Java things, Actually I've finished the basic now doing intermediate.

Here are the sources I've learned them.

Java - Beginner
http://www.thenewboston.org/list.php?cat=31

And now doing
Intermediate Java
http://www.thenewboston.org/list.php?cat=25

My question is can you suggest me a good Framework to study after these? As I see collection framework is covered in these but need to learn a good framework to do some useful stuff :)
I've heard Hibernate,Sprint and etc.. but don't know what is the best one for a beginner.

Anyway I am posting this on another thread and here is the link :)
https://robot.lk/viewtopic.php?f=12&t=3606






Thanks
Post Reply

Return to “Java Programming”