Program:
import java.util.*;
class ReverseString
{
public static void main(String args[])
{
String original, reverse = "";
Scanner in = new Scanner(System.in);
System.out.println("Enter a original String : ");
original = in.nextLine();
int length = original.length();
System.out.println("length is : "+length);
System.out.println("length is -1 is : "+(length-1));
System.out.println("original at positions are : "+original.charAt(4));
for ( int i = length - 1 ; i >= 0 ; i-- )
reverse = reverse + original.charAt(i);
System.out.println("Reverse of entered string is: "+reverse);
}
}
output:
Enter a original String :
ashok java
length is : 10
length is -1 is : 9
original at positions are : k
Reverse of entered string is: avaj kohsa
A couple of notes..
ReplyDelete- original.charAt(4) fails if the input is less than 5 characters,
- I strongly avoid declare variables before their valid initialization, if at all possible.
If it doesn't have a meaningful value, it is more correct for it not to be in scope! For this reason we should only declare 'original' where we first read it, & declare 'reverse' where first start building it.
Hope this helps.
Tom
http://literatejava.com/
Dear Thomas ,
ReplyDeleteThis is ujwala,what you said is right,but that fellow print the position of the value (just finding char purpose only ) he is printed that position...
thanks,
ujju