Java String Reverse

Sort by

recency

|

1879 Discussions

|

  • + 0 comments

    import java.io.; import java.util.;

    public class Solution {

    public static void main(String[] args) {
    
        Scanner sc=new Scanner(System.in);
        String A=sc.next();
        /* Enter your code here. Print output to STDOUT. */
        StringBuilder sb=new StringBuilder(A);
        String z=sb.reverse().toString();
        if(A.equals(z)){
            System.out.println("Yes");
        }else{
            System.out.println("No");
        }
    
    }
    

    }

  • + 0 comments

    Simplest ways without reverse() is : -

    import java.io.; import java.util.;

    public class Solution {

    public static void main(String[] args) {
    
        Scanner sc=new Scanner(System.in);
        String str=sc.next();
        /* Enter your code here. Print output to STDOUT. */
        String rev="";
        for(int i=str.length()-1;i>=0;i--){
            rev+=str.charAt(i);
        }
        if(rev.equals(str)){
            System.out.println("Yes");
        }
        else{
            System.out.println("No");
        }
    }
    

    }

  • + 0 comments
        StringBuffer sb = new StringBuffer(A);
        System.out.println(A.contentEquals(sb.reverse())? "Yes" : "No");
    
  • + 0 comments

    import java.io.; import java.util.;

    public class Solution {

    public static void main(String[] args) {
    
        Scanner sc=new Scanner(System.in);
        String A=sc.next();
        char[] cr = new char[A.length()];
        char[] cr1 = new char[A.length()];
        for(int i = 0;i < A.length(); i++){
            cr[i] = A.charAt(i);
        }    
    
        for(int i = 0; i < A.length();i++){
            cr1[A.length() - 1 - i] = cr[i];
        }    
    
        if(Arrays.equals(cr, cr1)){
            System.out.println("Yes");
        }else{
            System.out.println("No");
        }
    
    
    }
    

    }

    
    
    
    
  • + 0 comments

    import java.io.; import java.util.;

    public class Solution {

    public static void main(String[] args) {
    
        Scanner sc = new Scanner(System.in);
        String A = sc.next();
        String rev = ""; // Initialize the variable rev
    
        // Reverse the string
        for (int i = 0; i < A.length(); i++) {
            rev = A.charAt(i) + rev;                        
        }
    
        // Check if the string is a palindrome
        if (rev.equals(A)) {
            System.out.print("Yes");
        } else {
            System.out.print("No");
        }
    }   
    

    }