0

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);
    }
1

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.

Votes + Comments
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);
0

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.

1

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

Votes + Comments
I like nice people.
0

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

Edited by pbj.codez: I misspelled attempt

0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.