Program to find the frequency of character in a string in Kotlin
package com.includehelp.basic
import java.util.*
//Main Function, entry Point of Program
fun main(args: Array<String>) {
// InputStream to get Input
val scanner = Scanner(System.`in`)
//Input String
print("Enter String : ")
val str = scanner.nextLine()
//Input Character to check Frequency in Above input String
print("Enter Character : ")
val c = scanner.next()[0]
var frequency=0
//Count Frequency
for (i in str.indices){
if(c == str[i]){
frequency++
}
}
//Print Frequency of Character
println("Frequency of $c is : $frequency")
}
Output
RUN 1:
Enter String : include help team
Enter Character : e
Frequency of e is : 3
---
Run 2:
Enter String : Kotlin is the Modern Programming Languagae
Enter Character : n
Frequency of n is : 4
Program to find the frequency of character in a string in Kotlin
Output
need an explanation for this answer? contact us directly to get an explanation for this answer