Reverse a string in Java

前端 未结 30 2624
礼貌的吻别
礼貌的吻别 2020-11-21 13:12

I have \"Hello World\" kept in a String variable named hi.

I need to print it, but reversed.

How can I do this? I understand there

30条回答
  •  心在旅途
    2020-11-21 13:44

    package logicprogram;
    import java.io.*;
    
    public class Strinrevers {
    public static void main(String args[])throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("enter data");
        String data=br.readLine();
        System.out.println(data);
        String str="";
        char cha[]=data.toCharArray();
    
        int l=data.length();
        int k=l-1;
        System.out.println(l);
    
    
        for(int i=0;k>=i;k--)
        {
    
            str+=cha[k];
    
    
        }
        //String text=String.valueOf(ch);
        System.out.println(str);
    
    }
    
    }
    

提交回复
热议问题