-1

I want to make program for String palindrome without using built in functions.

Below is the code i have tried so far :

public class Palindrom
    {
        private static Scanner in;

        public static void main(String[] args)
        {
            String s,str1,str2;
            Scanner scan =new Scanner (System.in);
            System.out.println("Enter the string");
            String s = in.nextLine();
            StringBuffer str1 = new StringBuffer();
            StringBuffer str2 = new StringBuffer();
            str1.reverse();
            System.out.println("orignal string="+str2);
            System.out.println("reveser string="+str1);
             if(String.valueOf(str1).compareTo(String.valueOf(str2))==0)

                System.out.println("palindrom");

             else
                 System.out.println("not palindrom");
        }
    }

This program is not working properly.I think problem is in in.nextLine and string buffer.

Meenal
  • 2,879
  • 5
  • 19
  • 43
Prashant Rai
  • 7
  • 1
  • 1
  • 2
  • 2
    Please *read* the helpful tag pop-ups before slapping them on your post! This question has ***nothing*** to do with Swing! – Andrew Thompson Oct 01 '16 at 16:15
  • Please use code formatting for code and code snippets, structured documents like HTML/XML or input/output. To do that, select the text and click the `{}` button at the top of the message posting/editing form. – Andrew Thompson Oct 01 '16 at 16:15
  • 3
    Add some more description. Looks like you want to check if a string is palindrome. I see multiple problems: `String s` is not used anymore. Also if you say 'without using built in functions' then why are you using `StringBuffer.reverse()`. – Balkrishna Rawool Oct 01 '16 at 16:43
  • just for info, in java they call it method not function – Maytham Fahmi Oct 02 '16 at 03:35
  • At least, have you tried something (without built-in methods) ?? – Spotted Oct 04 '16 at 11:58

8 Answers8

0

The below given is the simplest way to check if the enteredenter code here string is a palindrome.

import java.util.Scanner;
public class Strng {
    public static void main(String[] args) {
    String r = ""; //To store the reverse
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter the String");
    String s = sc.next(); // Entering the string

    for(int i= s.length() - 1;i>=0;i--) {
        r = r + s.charAt(i);
    }

    if(r.equals(s)) {
        System.out.println("Is a palindrome");
    }
    else {
        System.out.println("Not a palindrome");
    }
    }

}

laplace
  • 656
  • 7
  • 15
0
public static void palindrome(String str){
        Map<Character,Integer> myMap = new HashMap<Character, Integer>();
        int characterCounter = 1;
        int index=-1;
        for(char ch : str.toCharArray()){
            index++;
            if(str.length()%2!=0 && index==Math.abs(str.length()/2)) continue;
            if(myMap.containsKey(ch))
                characterCounter = myMap.get(ch)+1;
            myMap.put(ch, characterCounter);
        }
        boolean flag = true;
        for(Character myMApVal: myMap.keySet()){
            if(myMap.get(myMApVal)%2!=0){
                flag=false;
                break;
            }               
        }
        if(!flag) System.out.println(str+" is not palindrome");
        else System.out.println(str+" is palindrome");      
}       
SagarS
  • 21
  • 1
  • 6
0
package practice;

import java.util.Scanner;

public class String_Palindrome {
    public static void main(String[] args) {
//String s, str1, str2;
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter the string:");
        String s = scanner.nextLine();
        StringBuffer str1 = new StringBuffer(s);
        StringBuffer str2 = new StringBuffer(s);
        str1.reverse();
        //System.out.println("Original String is:" + str2);
        //System.out.println("Reverse String is:" + str1);
        if (String.valueOf(str1).compareTo(String.valueOf(str2)) == 0)
            System.out.println("Given String is a palindrome");
        else
            System.out.println("Given String is Not a palindrome");
    }
}
0

The below code is the simplest way to check if the String is palindrome.

public class pal {
    static String palindrome(String str) {
        char[] array = str.toCharArray();
        for (int i = 0; i < array.length / 2; i++) {
            if (array[i] != array[array.length - i - 1]) {
                return "not a palindrome ";
            }
        }
        return "palindrome ";
    }
    public static void main(String args[]) {
        System.out.println(palindrome("11211"));
    }
}
Bhadra
  • 1
-1
    import java.util.Scanner;

    public class Palindrom {

        static Scanner scanner = new Scanner(System.in);

        public static void main(String[] args) {

            System.out.println("Enter the string");
            String inputStr =scanner.next();
            System.out.println("Given String = "+inputStr);
            char [] charArray=inputStr.toCharArray();
            int strlength=(charArray.length)-1;
            boolean isPalindrom=true;

            for(int count=0; count<charArray.length && strlength >= 0; count++,strlength--){
                if(charArray[count]!=charArray[strlength]){
                    isPalindrom=false;
                    break;
                }      
            }

            if(isPalindrom){
                System.out.println("palindrom");
            }else{
                System.out.println("not palindrom");
            }

        }
    }

Explaination :

  1. Given string to convert char array.
  2. Take two point

    1st beginning of the string a[0]

    2nd one ending of the string a[length-1]

  3. Compare every char in char array

    Compare beginning to end and ending begin while looping.

Palindrom case 1:(length is odd)

"madam" is Palindrom:

a[0] == a[4] m

a[1] == a[3] a

a[2] == a[2] d

a[3] == a[1] a

a[4] == a[0] m

Palindrom case 2:(length is even)

"malayalam" is Palindrom:

a[0] == a[8] m

a[1] == a[7] a

a[2] == a[6] l

a[3] == a[5] a

a[4] == a[4] y

a[5] == a[3] a

a[6] == a[2] l

a[7] == a[1] a

a[8] == a[0] m

Not Palindrom case 3 : Does not match in every chars( beginning to end and ending begin ).

-1
import java.util.Scanner;

public class StringPalindrome {

public static void main(String[] args) {
    Scanner sc = null;
    String str1 = null;
    // create object for scanner class
    sc = new Scanner(System.in);
    if (sc != null) {
        System.out.println("Enter First String");
        str1 = sc.nextLine();
    }
    strPalindrom(str1);
}// main

static void strPalindrom(String str) {
    // converting string into array
    char ch[] = str.toCharArray();
    // check string is Palindrom or not
    int count = ch.length - 1;
    for (int i = 0; i < ch.length; i++, count--) {
        if (ch[i] != ch[count]) {
            System.out.println("String is not Palindrom");
            break;
        } else {
            if (i == count) {
                System.out.println("String is Palindrom");
            }
        }
    } // for
}// strPalindrom method
}// class
Abhishek Gurjar
  • 7,426
  • 10
  • 37
  • 45
  • While providing code is fine, you should also provide a description on what your code is doing, and why you chose this solution. This way others can understand your thought processes used when creating your answer. – JRSofty Jun 19 '17 at 06:47
-1

I saw above all program but it bit hard not understandable to beginners.

Very Simple Program for beginners (to check String Palindrome)

public class Palindrome{
public static void main(String[] args) {
    String s = "malayalam"; // enter here your requred String to check Palindrom
    String rev = "";
    for (int i = s.length() - 1; i >= 0; i--) {
        rev = rev + s.charAt(i);
    }
    if (rev.equals(s)) {
        System.out.println("Palindrom");
    } else {
        System.out.println("Not Palindrom");
    }
  }

}
Onic Team
  • 1,620
  • 5
  • 26
  • 37
-3
public class Palindrom {

        public static void main(String[] args) {

        String str= "dad";
        char[] rev=str.toCharArray();
        int i=0;
        int j= rev.length-1;
        while(i<j){
            char temp =rev[i];
            rev[i] = rev[j];
            rev[j] = temp;
            i++;
            j--;
        }
        System.out.println(rev);
        System.out.println();
        if(str.equals(String.valueOf(rev))){
            System.out.println("Yes! It is palindrom");
        }else{
            System.out.println("No! It is not palindrom");
        }
    }

}
Samuil Petrov
  • 542
  • 1
  • 13
  • 24
  • 1
    While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. – rollstuhlfahrer Apr 02 '18 at 07:05