Q:

Kotlin program to check palindrome string

belongs to collection: Kotlin String Programs

0

What is a palindrome string?

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Kotlin program to count the occurrences of each ch... >>
<< Kotlin program to remove all whitespaces from the ...