• Post Reply Bookmark Topic Watch Topic
  • New Topic

reverse a string  RSS feed

 
lokesh rajarathnam
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello all,

i strucked in reverse a string program.... i tryed for only getting an string fron command prompt....

this the program i try...

public class Reverse {

public static void main(String args[]){
Reverse Rv=new Reverse();
String str1=null;
for(int i=0; i<args.length;i++){
str1=args[0];
System.out.println(" the length of the string is " +args.length);
System.out.println(" the entered string is =" +str1);
}

System.out.println(" the entered string is =" +(str1));
// System.out.println(" the entered string is =" +Rv.length(str1));


}
}

1. I want how to reverse the string.
2. How to find the length of the string ?
3.what the mistake in Rv.length(str1); ?
 
Sahid Khan
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not clear what is your problem? Do you want to reverse a string? However..

String does not have any method to reverse itself. But StringBuilder has a reverse function, refer to the apidoc.

String has a method length() which returns length of it, refer to the apidoc.

The code you have given, has no such method called length()?
 
sankar narayanan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi lokesh

1. You can find the length of the string using length() method.
2. First convert the String to StringBuffer, then reverse it using reverse(), and finally convert the StringBuffer to String using toString() method.

The modified version of your code to reverse a string


I am also started learning just now.
If anything wrong, Please don't mistake me.

Regards,
Sankar
 
James Tsai
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class reverseOrder {
public static void main(String[] args) {

String str = "The cow jumped over the moon";
System.out.println("Original string -->> " + str);

String[] srcArray = null;
srcArray = str.split(" ");
System.out.println("srcArray.length -->> " + srcArray.length );
System.out.print("Reverse string -->> ");
for (int i = srcArray.length - 1; i >= 0; i--) {
System.out.print(srcArray[i] + " ");
}

}
}
---------------
Original string -->> The cow jumped over the moon
srcArray.length -->> 6
Reverse string -->> moon the over jumped cow The
 
debashis ghosh
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try this:

public class Reverse {
public static void main(String args[]){
Reverse Rv=new Reverse();
String str1=null;
for(int i=0; i<args.length;i++){
str1=args[0];
System.out.println(" the length of the string is " +str1.length());
System.out.println(" the entered string is =" +str1);
}
String revStr = "";
for(int i=str1.length()-1;i>=0;i--){
revStr = revStr+str1.charAt(i);
}
System.out.println("reversed string-->"+revStr);}
}
 
debashis ghosh
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try this:

public class Reverse {
public static void main(String args[]){
Reverse Rv=new Reverse();
String str1=null;
for(int i=0; i<args.length;i++){
str1=args[0];
System.out.println(" the length of the string is " +str1.length());
System.out.println(" the entered string is =" +str1);
}
String revStr = "";
for(int i=str1.length()-1;i>=0;i--){
revStr = revStr+str1.charAt(i);
}
System.out.println("reversed string-->"+revStr);}
}
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!