Tech Support Guy banner

reverse a string in java

1399 Views 2 Replies 2 Participants Last post by  punjabian263
Hello guys i want to reverse a string in java.e.g
"hello how are u"
it should be displayed as
"u era woh olleh"
i have tried this but unable.

Code:
                                String s="hello how are you";
		String temp=s;
		int j=0;
		for(int i=s.length();i>=0;i--)
		{
			s.chatAt(i)=temp.charAt(j);
			j++;
		}
Status
Not open for further replies.
1 - 1 of 3 Posts
Give this a shot

Code:
public class StringReverse {
    public static void main(String[] args) {
        String text = "hello how are you";
        char reversedText[] = new char[text.length()];
        for (int i = 0; i < text.length(); i++)
            reversedText[i] = text.charAt(text.length() - i - 1);
        String reversedTextString = new String(reversedText);
        System.out.print(reversedTextString);
   }
}
See less See more
1 - 1 of 3 Posts
Status
Not open for further replies.
Top