palindrome program in java

orangekoala793

New member
Chương trình ### palindrom ở Java

Một palindrom là một chuỗi đọc cùng một ngược và về phía trước.Ví dụ: "Trường đua" là một palindrom.Trong hướng dẫn này, chúng tôi sẽ học cách viết một chương trình palindrom ở Java.

Chúng tôi sẽ bắt đầu bằng cách tạo một dự án Java mới.Khi dự án được tạo, chúng tôi sẽ tạo một lớp mới có tên là `palindrom`.Trong lớp này, chúng tôi sẽ viết phương thức sau:

`` `java
công khai boolean ispalindrom (chuỗi str) {
int n = str.length ();
for (int i = 0; i <n / 2; i ++) {
if (str.charat (i)! = str.charat (n - i - 1)) {
trả lại sai;
}
}
trả lại đúng;
}
`` `

Phương thức này lấy một chuỗi làm tham số và trả về giá trị boolean.Phương pháp trước tiên kiểm tra xem độ dài của chuỗi là chẵn.Nếu không, thì chuỗi không thể là một palindrom.

Phương pháp sau đó lặp qua chuỗi từ đầu đến giữa.Đối với mỗi ký tự trong chuỗi, phương thức kiểm tra xem ký tự ở đầu đối diện của chuỗi có giống nhau không.Nếu các ký tự không giống nhau, thì phương thức trả về sai.

Nếu phương thức đạt đến giữa chuỗi mà không tìm thấy bất kỳ ký tự không khớp nào, thì phương thức sẽ trả về đúng.

Bây giờ chúng ta có thể kiểm tra phương thức của mình bằng cách tạo một lớp `main` mới.Trong lớp này, chúng tôi sẽ viết mã sau:

`` `java
công khai void void main (String [] args) {
Chuỗi str = "Trường đua ngựa";
System.out.println (ispalindrom (str));// ĐÚNG VẬY

str = "xin chào";
System.out.println (ispalindrom (str));// SAI
}
`` `

Khi chúng tôi chạy mã này, chúng tôi sẽ thấy đầu ra sau:

`` `
ĐÚNG VẬY
SAI
`` `

Điều này cho thấy phương pháp của chúng tôi đang hoạt động chính xác.

### hashtags

* #xuôi ngược đều giống nhau
* #Java
* #Programming
* #dây
* #Algorithms
=======================================
### Palindrome Program in Java

A palindrome is a string that reads the same backwards and forwards. For example, "racecar" is a palindrome. In this tutorial, we will learn how to write a palindrome program in Java.

We will start by creating a new Java project. Once the project is created, we will create a new class called `Palindrome`. In this class, we will write the following method:

```java
public static boolean isPalindrome(String str) {
int n = str.length();
for (int i = 0; i < n / 2; i++) {
if (str.charAt(i) != str.charAt(n - i - 1)) {
return false;
}
}
return true;
}
```

This method takes a string as a parameter and returns a boolean value. The method first checks if the length of the string is even. If it is not, then the string cannot be a palindrome.

The method then iterates through the string from the beginning to the middle. For each character in the string, the method checks if the character at the opposite end of the string is the same. If the characters are not the same, then the method returns false.

If the method reaches the middle of the string without finding any mismatched characters, then the method returns true.

We can now test our method by creating a new `Main` class. In this class, we will write the following code:

```java
public static void main(String[] args) {
String str = "racecar";
System.out.println(isPalindrome(str)); // true

str = "hello";
System.out.println(isPalindrome(str)); // false
}
```

When we run this code, we will see the following output:

```
true
false
```

This shows that our method is working correctly.

### Hashtags

* #palindrome
* #Java
* #Programming
* #strings
* #Algorithms
 
Join ToolsKiemTrieuDoGroup
Back
Top
AdBlock Detected

We get it, advertisements are annoying!

Sure, ad-blocking software does a great job at blocking ads, but it also blocks useful features of our website. For the best site experience please disable your AdBlocker.

I've Disabled AdBlock