6 Replies - 844 Views - Last Post: 07 October 2014 - 01:32 PM Rate Topic: -----

#1 javanoob17  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 86
  • Joined: 10-July 14

reverse a string in java

Posted 07 October 2014 - 12:05 PM

Hello i am having a hard time with this program. I was wondering if something could tell me if i am doing this correctly according to the requirements. Thank you for reviewing my post and contributing your time and effort.

Here is my code:

public class ReverseString {
 
 
    public static void main(String args[]) {
     
        //quick wasy to reverse String in Java - Use StringBuffer
        String str = "The rain in Spain falls mainly on the plain";
        String revString = new StringBuffer(str).reverse().toString();
        System.out.println(str);
        System.out.println(revString);

    }  
 
 
    public static String reverse(String string){
        if(string == null || string.isEmpty()){
            return string;
        }      
        String revString = "";
        for(int d = string.length() -1; d<=0; d--){
            revString = revString + string.charAt(d);
        }
     
        return revString;
    }
   
}


The requirements:

The program runs but for some reason it is not meeting the requirements. use the String method toCharArray and a loop to display the letters in the string in reverse order.

Is This A Good Question/Topic? 0
  • +

Replies To: reverse a string in java

#2 Koekje  Icon User is offline

  • New D.I.C Head

Reputation: 20
  • View blog
  • Posts: 45
  • Joined: 24-October 13

Re: reverse a string in java

Posted 07 October 2014 - 12:20 PM

public static String reverse(String string){
        if(string == null || string.isEmpty()){
            return string;
        }      
        String revString = "";
        for(int d = string.length() -1; d<=0; d--){
            revString = revString + string.charAt(d);
        }
     
        return revString;
    }
   
}


You are not calling this method and neither are you using the method toCharArray.

1 thing to note as well, your for loop is wrong. You are running the loop while d <= 0, think about that.
Was This Post Helpful? 0
  • +
  • -

#3 javanoob17  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 86
  • Joined: 10-July 14

Re: reverse a string in java

Posted 07 October 2014 - 12:45 PM

Is this better?

public class ReverseOrder {
 
    public static void main(String[] args) {
        String str = "The rain in Spain falls mainly on the Plain";
        System.out.println(str);
        System.out.println(reverseStringUsingLoop(str));
    }
 
    private static String reverseStringUsingLoop(String str) {
        char[] charArray = str.toCharArray();
        StringBuilder builder = new StringBuilder();
        for (int d = charArray.length - 1; d >= 0; d--) {
            builder.append(charArray[d]);
        }
        return builder.toString();
    }
}

Was This Post Helpful? 0
  • +
  • -

#4 Koekje  Icon User is offline

  • New D.I.C Head

Reputation: 20
  • View blog
  • Posts: 45
  • Joined: 24-October 13

Re: reverse a string in java

Posted 07 October 2014 - 01:00 PM

Yes, looks good!

Perhaps reversing the charArray in place would be better performance wise, but the code is very readable now and works, which is the most important point.

This post has been edited by Koekje: 07 October 2014 - 01:01 PM

Was This Post Helpful? 0
  • +
  • -

#5 javanoob17  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 86
  • Joined: 10-July 14

Re: reverse a string in java

Posted 07 October 2014 - 01:08 PM

How would i go about using the split() method for reversing the strings output? Would i just replace .toCharArray with .split?
Was This Post Helpful? 0
  • +
  • -

#6 javanoob17  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 86
  • Joined: 10-July 14

Re: reverse a string in java

Posted 07 October 2014 - 01:29 PM

Hello All,

Thank you for viewing my post and any contributions you provide. I am having a hard time with the concept with of split string. I just need to use the split string method.


import java.util.Scanner;

public class SplitString {
 
 
    public static void main(String args[]) {
     
        String string;
        String[] tokens = string.split(" ");
        Scanner input = new Scanner(System.in);
        System.out.print("Please enter a string of words: ");
        string = input.nextLine();
        System.out.println(string);
        System.out.println(reverseStringUsingLoop(string));

    }  
 
 
    private static String reverseStringUsingLoop(String string) {
        char[] charArray = string.toCharArray();
        StringBuilder builder = new StringBuilder();
        for (int d = charArray.length - 1; d >= 0; d--) {
            builder.append(charArray[d]);
        }
        return builder.toString();
    }
   
}


*********returns the phrase with the words in reverse order. Use the String class's .split() method for this.*********

This post has been edited by javanoob17: 07 October 2014 - 01:30 PM

Was This Post Helpful? 0
  • +
  • -

#7 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13394
  • View blog
  • Posts: 53,453
  • Joined: 12-June 08

Re: reverse a string in java

Posted 07 October 2014 - 01:32 PM

Please do not create duplicate topics. Merging.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1