kotlin中判断字符串
Given a string and a character, we have to find the frequency of the character in the string.
给定一个字符串和一个字符,我们必须找到字符串中字符的频率。
Example:
例:
Input:
string = "IncludeHelp"
character to find = 'e'
Output:
2
在Kotlin中查找字符串中字符频率的程序 (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
翻译自: https://www.includehelp.com/kotlin/find-the-frequency-of-character-in-a-string.aspx
kotlin中判断字符串