Program to check whether two strings are anagram of each other in Kotlin
package com.includehelp.basic
import java.util.*
/**
* Function to check two strings are anagram string or not
*/
fun isAnagrams(str1: String, str2: String): Boolean {
//Both String Length must be Equal
if (str1.length != str2.length) {
return false
}
//Convert Strings to character Array
val strArray1 = str1.toCharArray()
val strArray2 = str2.toCharArray()
//Sort the Arrays
Arrays.sort(strArray1)
Arrays.sort(strArray2)
//Convert Arrays to String
val sortedStr1 = String(strArray1)
val sortedStr2 = String(strArray2)
//Check Both String Equals or not After Sorting
//and Return value True or False
return sortedStr1 == sortedStr2
}
//Main Function, entry Point of Program
fun main(args: Array<String>) {
//Input Stream
val sc = Scanner(System.`in`)
//Enter String Value
println("Enter String1 : ")
val str1: String = sc.nextLine()
println("Enter String2 : ")
val str2: String = sc.next()
//Call Function to check anagram String
if (isAnagrams(str1, str2)) {
println("Anagram Strings !!")
} else {
println("Strings are not Anagram !!")
}
}
Output
Run 1:
Enter String1 :
raghu
Enter String2 :
gharu
Anagram Strings !!
---
Enter String1 :
Hello
Enter String2 :
includehelp
Strings are not Anagram !!
---
Enter String1 :
john
Enter String2 :
hnoj
Anagram Strings !!
Program to check whether two strings are anagram of each other in Kotlin
Output
need an explanation for this answer? contact us directly to get an explanation for this answer