+ 16
Can anyone please help me with this solving this code in Kotlin. I tried a lot but it's not going well.
You need to define a function, which takes a letter and a text as arguments, and returns the number of times the given letter appears in the given text. Sample Input: e awesome Sample Output: 2 fun main(args: Array<String>) { val letter: Char = readLine()!![0] val text: String = readLine()!! val result = letter_count(letter, text) println(result) }
68 Réponses
+ 29
fun main() {
val c = readLine()!!
val txt = readLine()!!
print(txt.filter(){
it.toString().equals(c)}
.count())
}
If you want to ignore case, use: .equals(c, true)
https://code.sololearn.com/c5JZpc8gg482/?ref=app
+ 6
//I guess this will help you
fun letterCount(letter: String, text: String): Int {
return text.toList().count{ it == letter.single() }
}
+ 5
Thank you Coding Cat [Mouse searching]
+ 3
Sorry, didn't read your post right. Have changed my solution to a funktion with return value 😉
https://code.sololearn.com/c5JZpc8gg482/?ref=app
+ 2
Hi
+ 2
Thanks Ananya. It didn't figure out correctly.
However, I was able to sort it out. Here it is:
fun main(args: Array<String>) {
val ch :Char = readLine()!![0]
val str : String = readLine()!!
var frequency = 0
for (i in 0..str.length - 1) {
if (ch == str[i]) {
++frequency
}
}
println("$frequency")
}
+ 2
Ananiya Jemberu
nothing! I've tryed your solution. It works fine for me 👍
Didn't know this .single() funktion since now. Very nice 😀
+ 2
yes, the force is with me.
+ 2
fun main(args: Array<String>) {
val ch :Char = readLine()!![0]
val str : String = readLine()!!
var frequency = 0
for (i in 0..str.length - 1) {
if (ch == str[i]) {
++frequency
}
}
println("$frequency")
}
+ 1
Ananiya Jemberu , thank you so much. I appreciate a lot for approaching for help
+ 1
Fardina Kabir what appears wrong, jut to learn from my mistake :P
edited: i have tried and seems to work https://code.sololearn.com/cL5o4xEl2w88/?ref=app
+ 1
Ow, thanks :) Coding Cat [Mouse searching]
it just convert single string to char
+ 1
fun letter_count(letter:Char, text:String) : Int {
var count: Int = 0
for (c in text) {
if (c == letter) count += 1
}
return count
}
fun main(args: Array<String>) {
val letter: Char = readLine()!![0]
val text: String = readLine()!!
val result = letter_count(letter, text)
println(result)
}
+ 1
fun main() {
val c = readLine()!!
val txt = readLine()!!
var count = letterCount(c, txt)
println(count)
println(letterCount(c, txt))
}
fun letterCount(c: String, txt: String): Int{
return (txt.filter(){
it.toString().equals(c)}.count())
}
+ 1
fun main() {
val c = readLine()!!
val txt = readLine()!!
var count = letterCount(c, txt)
println(count)
println(letterCount(c, txt))
}
fun letterCount(c: String, txt: String): Int{
return (txt.filter(){
it.toString().equals(c)}.count())
}
+ 1
Obi Wan is here to help i have force also
+ 1
I am the best Skywalker!!!
+ 1
fun main(args: Array<String>) {
val ch :Char = readLine()!![0]
val str : String = readLine()!!
var frequency = 0
for (i in 0..str.length - 1) {
if (ch == str[i]) {
++frequency
}
}
println("$frequency")
}
+ 1
Thx Mohamed Yassen
+ 1
To solve this problem in Kotlin, you can define a function letter_count that takes a letter and a text as arguments, and returns the number of times the given letter appears in the given text. Here is an example of how you can implement this function:
fun letter_count(letter: Char, text: String): Int {
var count = 0
for (c in text) {
if (c == letter) {
count++
}
}
return count
}
This function uses a loop to iterate over each character in the text string, and increments a counter count whenever it finds a character that is equal to the given letter. After the loop completes, the function returns the final value of count.
You can then use this function in the main function as follows:
fun main(args: Array<String>) {
val letter: Char = readLine()!![0]
val text: String = readLine()!!
val result = letter_count(letter, text)
println(result)
}