-1

In a given sentences, reverse each word and print the reversed words in the sentences. if there is a palindrome print those words .if there is no palindrome print "No Palindrome". this is what i wrote

import java.util.Scanner;   
public class main{   
    public static void main(String[] args){     
        Scanner input=new Scanner(System.in);  
        String s1=input.nextLine();   
        String arr[]=s1.split("\\s",s1.length());   
        int count=0;    
        String palindrome[]=new String[s1.length()];    

        for(int i=0;i<arr.length;i++){



             String s2="";
          for(int j=arr[i].length()-1;j>=0;j--){
          s2=s2.concat(Character.toString(arr[i].charAt(j)));
          System.out.println(arr[i].charAt(j));
          }
          System.out.print(" ");
          if(arr[i].equals(s2)){
              count++;
              palindrome[i]=s2;
          }

        }

        if(count>0){
             for(String i:palindrome)
             System.out.println(i);}
             else 
             System.out.println("Not a palindrome");
        }

    }

But code is not giving proper output.

  • Please indent your code and use whitespace within lines according to the standard Java style conventions. – Stephen C Sep 21 '19 at 10:57
  • 1
    *"But code is not giving proper output."* - For a given input, please show us what the program is outputting, and what you are expecting it to output. – Stephen C Sep 21 '19 at 11:10
  • I can already see two potential problems related to the `palindrome` array. I predict you're getting a `NullPointerException` at the very end when you're trying to print the results. Did I guess correctly? – Kevin Anderson Sep 21 '19 at 11:12

3 Answers3

1

This should work:

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    String s1 = input.nextLine();
    String[] arr = s1.split(" ");
    StringBuilder output = new StringBuilder();
    for (String currentWord : arr) {
        String reverseWord = new StringBuilder(currentWord).reverse().toString();
        if (currentWord.equals(reverseWord)) {
            output.append(reverseWord);
        } else {
            output.append("No Palindrome ");
        }
    }
    System.out.println(output);
}
Willem
  • 992
  • 6
  • 13
0

Here is a working code that you can use for checking the input string/number is palindrome or not palindrome

code:

    import java.util.*;
    class stackOverflow {
    
        public static void main(String[] args) {
            String original, reverse = ""; // Objects of String class  
            Scanner in = new Scanner(System.in);   
            System.out.println("Enter a string/number to check if it is a palindrome or not:");  
            // takes input string
            original = in.nextLine();   
            int length = original.length();   // length of the string to do iteration on it
            // check the string from the end to the start to reverse it
            // read and append it with the reverse variable in backward
            for ( int i = length - 1; i >= 0; i-- )  
               reverse = reverse + original.charAt(i);

            // Finally we check if the input string and reversed string
            if (original.equals(reverse))  
               System.out.println("Is palindrome.");  
            else  
               System.out.println("Not palindrome.");   
         }
    }

I've edited your code and now it's working fine:

  import java.util.*;
    class stackOverflow {
        public static void main(String[] args){     
            Scanner input=new Scanner(System.in);  
            String s1=input.nextLine();   
            String arr[]=s1.split("\\s",s1.length());   
    
            for(int i=0;i<arr.length;i++){
    
              String s2="";
              for(int j=arr[i].length()-1;j>=0;j--){
              s2=s2.concat(Character.toString(arr[i].charAt(j)));
              //System.out.println(arr[i].charAt(j));
              }
              //System.out.print(" ");
              if(arr[i].equals(s2)){
                  //palindrome[i]=s2; // you are inserting the s2 value into the first element of the array, 
                  //so the rest of the positions remains empty/null that's not a problem to solve palindrome
                  
                System.out.println("Is a palindrome");
              }
              else 
                 System.out.println("Not a palindrome");
            }
            
        }
    }
Nimantha
  • 6,405
  • 6
  • 28
  • 69
A S M Sayem
  • 2,010
  • 2
  • 21
  • 28
0

Here's a shorter way of doing this:

//prints each palindome word in a sentence
Arrays.asList(sentence.split(" ")).stream().filter(this::isPalindrome).forEach(System.out::println);
Prashant Pandey
  • 4,332
  • 3
  • 26
  • 44