• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

reverse a string

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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); ?
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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()?
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);}
}
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic