Home »
Kotlin »
Kotlin programs »
Kotlin string programs
Kotlin program to check palindrome string
Kotlin | Check palindrome string: Here, we are going to learn how to check whether a given string is a palindrome string or not in Kotlin programming language?
Submitted by IncludeHelp, on April 29, 2020
What is a palindrome string?
A palindrome string is the string that is equal to its reverse string i.e. if the string read from left to right is equal to string reads from right to left is called a palindrome string.
For example: "radar" is a palindrome string.
Given a string, we have to check whether string is palindrome string or not.
Example:
Input:
string = "radar"
Output:
"radar" is a palindrome string.
Input:
string = "hello"
Output:
"hello" is not a palindrome string.
Program to check palindrome string in Kotlin
In the below program, at the first we are reading a string and then reversing the string, finally comparing the original string with the reverse string. If both are equal then the string will be a palindrome string.
package com.includehelp.basic
import java.util.*
//function to check string is palindrome or not
fun isPalindromeString(inputStr: String): Boolean {
val sb = StringBuilder(inputStr)
//reverse the string
val reverseStr = sb.reverse().toString()
//compare reversed string with input string
return inputStr.equals(reverseStr, ignoreCase = true)
}
//Main function, Entry Point of Program
fun main(args: Array<String>) {
//Input Stream
val sc = Scanner(System.`in`)
//Input String Value
println("Enter String : ")
val inString: String = sc.nextLine()
//Call function to check String
if (isPalindromeString(inString)) {
println("$inString is a Palindrome String")
} else {
println("$inString is not a Palindrome String")
}
}
Output
Run 1:
Enter String :
includehelp
includehelp is not a Palindrome String
---
Run 2:
Enter String :
Malayalam
Malayalam is a Palindrome String