Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Reverse a String

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Reverse a String

     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 the word lenght.. how can i get rid of it? thanks


  2. #2
    Junior Member
    Join Date
    Aug 2012
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reverse a String

    you must use for (int i=0;i<result.length();i++)
    then your problem will be solved

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Reverse a String

    Where is the method: lenght() defined? The compiler can not find it in the code.
    What value do you want to use to control the for loop?

    @maz3r Did you test the code you posted?
    Last edited by Norm; August 21st, 2012 at 08:49 AM.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Reverse a String

    Mehwish-S, please read the forum rules, and do not double post. Your other thread has been deleted.

  5. #5
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: Reverse a String

    This thread has been cross posted here:

    http://www.java-forums.org/new-java/62400-reverse-string.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting


Similar Threads

  1. [SOLVED] Method to reverse String using While Loop
    By Montrell79 in forum Loops & Control Statements
    Replies: 2
    Last Post: February 22nd, 2012, 03:49 PM
  2. Reverse every pair of words in a string
    By mulligan252 in forum Collections and Generics
    Replies: 4
    Last Post: October 11th, 2011, 04:29 PM
  3. Reverse String
    By WantHelp in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 5th, 2011, 06:14 AM
  4. How to reverse a string, skiping numbers , and ' ?
    By mlotfi in forum Algorithms & Recursion
    Replies: 2
    Last Post: May 26th, 2011, 05:13 AM