Java String Reverse

Sort by

recency

|

1867 Discussions

|

  • + 0 comments

    works fine

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

    public class Solution {

    public static String palindrome(String A){
        char[] char_array = A.toCharArray();
        int i = 0;
        int j = A.length()-1;
        while(i<j){
         if(char_array[i]!=char_array[j]){
             return "No";
         }
         i++;
         j--;
        }
        return "Yes";
    
        }
    
    
    
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String A = scanner.next();
        System.out.println(palindrome(A));
    

    } }

  • + 0 comments

    Surelly not the best solution, but it was fun public static void main(String[] args) {

        Scanner sc=new Scanner(System.in);
        String A=sc.next();
        char[] chars = A.toCharArray();
        int correctLength = chars.length % 2 == 0 ? chars.length/2 : chars.length/2 + 1;
        char[] reverseChars = new char[correctLength];
    
        /* Enter your code here. Print output to STDOUT. */
        for(int i = 0; i < correctLength; i++) {
            reverseChars[i] = chars[chars.length - i - 1];
        }
        if(new String(reverseChars).equals(A.substring(0, correctLength))){
            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();
            StringBuilder s = new StringBuilder(A);
            System.out.println(s.reverse().toString().equals(A) ? "Yes" : "No");
            
        }
    }
    
  • + 0 comments

    Solution :-

    1. First you should convert the string to its reversed order.
    2. Then compare both the strings.
    3. If the result is true, print Yes esle print No.
    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 s = "";
            /* Enter your code here. Print output to STDOUT. */
            for (int i = A.length() - 1; i >= 0; i--) {
                s += A.charAt(i);
            }
            if(A.equals(s)){
                System.out.println("Yes");
            }else{
                System.out.println("No");
            }
        }
    }
    
  • + 1 comment

    Solution

    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 B = "";
            char tmp;
            for (int i=0; i < A.length(); i++){
                tmp = A.charAt(i);
                B = tmp + B;
            }
            if (A.compareTo(B) == 0)
                System.out.println("Yes");
            else
                System.out.println("No");
        }
    }