Hi this is the solution that i came up with to inverse a string

public static void main(String[] args) {
        // TODO Auto-generated method stub

        String b="bhashitha";

        b.length();
        int p=0;
        String inverse="";
        while(p<b.length()){
        inverse+=b.charAt(b.length()-1-p);

            p++;

        }

        System.out.println(inverse);
    }

bhahista .. that would indeed work, you do have statements you don't use/need, but hey, if it works, it works, right? but anyway, after three years, if the OP hadn't found a way to do it yet, I'm pretty sure (s)he isn't looking anymore.

commented: hey first of all thnks for dive through my code..any way it may be having some useless things.but as u said it is working and i would like to say that this is my first post :) +0
    StringBuilder sb = new StringBuilder(s3);
    sb.reverse();
    System.out.println(sb);
    s3=sb.toString();
    System.out.println(s3);

gauravbit70: allow me to introduce you to a new (still experimental) concept. it's called 'Date'.
you are replying to a thread in which, according to the 'Date' (see how it kicks in?) the last post was almost half a year ago.
that was a post of mine. did you read that? it stated (and I quote)

but anyway, after three years, if the OP hadn't found a way to do it yet, I'm pretty sure (s)he isn't looking anymore.

you want to hear the joke on this? "How to reverse a String" well, actually, you can't. immutable, remember? you just create a new one, the original one won't be reversed anyway.

but still ... my guess is the OP stopped looking for answers about three years ago. maybe we should too.

Hello gauravbit70, welcome to DaniWeb.
Don't be put off by Stultuske, he's just having a bit of fun. Your contributions are very welcome here.
Before posting please do check the dates on threads, and please don't just post solutions with no explanations or comments - we want people to be able to learn from what they read here.
J

commented: I like nice people. +14

Hello. I am aware that this post is outdated, however; I would attempt this problem like so:

package palindrometest;

import java.util.Scanner;

/**
 *
 * @author PBJ
 */
public class PalindromeTest {
    static Scanner console = new Scanner(System.in);  
    public static void main(String[] args) {
        String userInput = getUserInput();
        String reversedInput = reverseUserInput(userInput);
        testForPalindrome(userInput, reversedInput);
    }

    private static String getUserInput() {
        System.out.println("Please enter your String to test");
        String tempString = console.nextLine();
        return tempString;
    }

    private static String reverseUserInput(String userInput) {
        String tempString = new StringBuilder(userInput).reverse().toString();
        System.out.println(tempString);
        return tempString;
    }

    private static void testForPalindrome(String userInput, String reversedInput) {
        if(reversedInput.equals(userInput)){
            System.out.println("Palindrome");
        }
        else{
            System.out.println("Not a palindrome");
        }
    }
}

My version is pretty self explanatory, but I may have misunderstood what I palindrome is. Is it a string that is spelt the same backwards as it is forwards? In that case then I feel that I have addressed the OP's question. If not disregard my attempt, and please don't hurt me

I would say that's a good example of using single-purpose methods methods and well-chosen names. It makes the code so clear that it needs no comments, and it's really easy to read, understand, and verify.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.