打印字符串反向

时间:2017-11-04 06:17:08

标签: string printing reverse

我想写一个程序,用户输入" asdfg"程序应该打印" gfdsa"我怎样才能做到这一点?我对此一无所知。

String s;
String rev;

Scanner sc = new Scanner (System.in);

System.out.println ("Enter 's' String : ");
s = sc.next();

System.out.println ("reverse of " + s + " equal to " + rev);

2 个答案:

答案 0 :(得分:0)

   System.out.println ("reverse of " + s + " equal to " + new StringBuilder(s).reverse().toString());

答案 1 :(得分:0)

试试这个,它会起作用

        string input = "sample text";
        char[] outputArray = input.ToCharArray();           
        Array.Reverse(outputArray);
        Console.WriteLine(outputArray);