Java implementation to determine whether palindrome

Description Title: "palindromic sequence" is positive and negative are the same string, such as "level", "moon", etc., is determined if it is a palindromic sequence of inserting a new element into the new string so that the string palindrome

Ideas:

  1. It prompts the user to enter a string
  2. It becomes one of string char
  3. Traversing a pointer from front to back, a pointer from the forward traverse, when the characters found not equal description is not palindromic sequence
  4. To find the insertion position: the second string from the string start position of the first attempt to insert a known method of determining i.e. palindromic locate the insert position returns true

The whole process is very simple, did not talk much to say, look at the code 

 Code shows:

package com.bittech.Test;

import java.util.Scanner;

/**
 * package:com.bittech.Test
 * Description:huiwen
 * @date:2019/5/22
 * @Author:weiwei
 **/
public class Test0522 {
    public static boolean isHuiwen(String s) {
        int i = 0;
        int j = s.length() - 1;
        while (i < j) {
            if (s.charAt(i) != s.charAt(j)) {
                return false;
            }
            i++;
            j--;
        }
        return true;
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入字符串:");
        String str1 = sc.nextLine();
        String str2 = sc.nextLine();
        int count = 0;
        for(int i = 0;i<str1.length();i++){
            StringBuilder sb = new StringBuilder(str1);
            sb.insert(i,str2);
            if(isHuiwen(sb.toString())){
                count++;
            }
        }
        System.out.println(count);
    }
}

 

Guess you like

Origin blog.csdn.net/weixin_43224539/article/details/90579480