Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: Reverse a String

  1. #1
    Mehwish-S is offline Member
    Join Date
    Aug 2012
    Posts
    12
    Rep Power
    0

    Default Reverse a String

    Create a class called StringFlip containing a single method called reverse, which takes one String as an argument and returns a String (this method should not be declared static). The method body should take the passed String, reverse it and return this inverted String.

    Java Code:
     package mocktest;
    
    public class StringFlip {
        
        public String reverse(String x) {
            
    	String result = "";
    
    	for (int i = 0; i < lenght(); i++) {
    
    	    result = x.charAt(i) + result;
    	}
    	return result;
    	    }
    
        public static void main(String args[]) {
    
    	StringFlip f = new StringFlip();
    	System.out.println(f.reverse ("Hello"));
        }}
    I have a stupid error under lenght! why?

  2. #2
    Mehwish-S is offline Member
    Join Date
    Aug 2012
    Posts
    12
    Rep Power
    0

    Default Reverse a String

    Create a class called StringFlip containing a single method called reverse, which takes one String as an argument and returns a String (this method should not be declared static). The method body should take the passed String, reverse it and return this inverted String.

    Java code:

    Java Code:
     package mocktest;
    
    public class StringFlip {
        
        public String reverse(String x) {
            
    	String result = "";
    
    	for (int i = 0; i < lenght(); i++) {
    
    	    result = x.charAt(i) + result;
    	}
    	return result;
    	    }
    
        public static void main(String args[]) {
    
    	StringFlip f = new StringFlip();
    	System.out.println(f.reverse ("Hello"));
        }}
    I have an error under lenght! how can i get rid of it?

  3. #3
    jhuber151 is offline Member
    Join Date
    Jul 2012
    Posts
    55
    Rep Power
    0

    Default Re: Reverse a String

    Well the first issue is length is spelled incorrectly. Next you are not specifying what variable you are taking the length of.
    Try:
    Java Code:
    for (int i = 0; i < x.length(); i++)

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,196
    Rep Power
    19

    Default Re: Reverse a String

    Please go through the Forum Rules -- particularly the second paragraph.

    I've merged your two threads and moved this to New to Java. Please don't post beginner questions in Advanced Java.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Reverse a String

    He's also cross-posted this elsewhere as well. OP, please see my note on this in your other question.

  6. #6
    Mehwish-S is offline Member
    Join Date
    Aug 2012
    Posts
    12
    Rep Power
    0

    Default Re: Reverse a String

    Quote Originally Posted by DarrylBurke View Post
    Please go through the Forum Rules -- particularly the second paragraph.

    I've merged your two threads and moved this to New to Java. Please don't post beginner questions in Advanced Java.

    db
    ok! sorryyy!

  7. #7
    Mehwish-S is offline Member
    Join Date
    Aug 2012
    Posts
    12
    Rep Power
    0

    Default Re: Reverse a String

    Quote Originally Posted by jhuber151 View Post
    Well the first issue is length is spelled incorrectly. Next you are not specifying what variable you are taking the length of.
    Try:
    Java Code:
    for (int i = 0; i < x.length(); i++)
    Hey! :D thank you so muchhhh.. it worksssssss

  8. #8
    bob 6 is offline Java Rookie
    Join Date
    Aug 2012
    Posts
    20
    Rep Power
    0

    Default Re: Reverse a String

    next time, you should use a IDE like Eclipse or NetBeans... it will check automatically for syntax errors.

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,196
    Rep Power
    19

    Default Re: Reverse a String

    Quote Originally Posted by bob 6 View Post
    next time, you should use a IDE like Eclipse or NetBeans... it will check automatically for syntax errors.
    To suggest adding the learning curve of an IDE is poor advice for someone who's still learning the Java language, .

    db
    quad64bit likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 10
    Last Post: 03-15-2012, 10:46 AM
  2. reverse string
    By mallikanala in forum New To Java
    Replies: 1
    Last Post: 01-21-2012, 03:40 PM
  3. How to reverse a string?
    By Neeer in forum New To Java
    Replies: 17
    Last Post: 03-27-2011, 08:10 PM
  4. Reverse A String Without Allocating A New String
    By marco.c84 in forum Advanced Java
    Replies: 10
    Last Post: 03-22-2011, 05:39 AM
  5. Reverse a string?
    By cysquatch in forum New To Java
    Replies: 15
    Last Post: 03-23-2010, 02:31 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •