+ 14

# 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) }

8th Apr 2021, 5:43 PM
Fardina Kabir
+ 27
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
8th Apr 2021, 7:27 PM
Coding Cat
+ 5
//I guess this will help you fun letterCount(letter: String, text: String): Int { return text.toList().count{ it == letter.single() } }
8th Apr 2021, 6:29 PM
Ananiya Jemberu
8th Apr 2021, 7:28 PM
Fardina Kabir
+ 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
8th Apr 2021, 7:50 PM
Coding Cat
+ 2
Hi
8th Apr 2021, 5:53 PM
Fardina Kabir
+ 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") }
8th Apr 2021, 7:17 PM
Fardina Kabir
+ 2
Ananiya Jemberu nothing! I've tryed your solution. It works fine for me 👍 Didn't know this .single() funktion since now. Very nice 😀
8th Apr 2021, 8:01 PM
Coding Cat
+ 2
yes, the force is with me.
24th May 2021, 9:30 AM
Luke Skywalker
+ 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") }
9th Aug 2022, 5:42 AM
srestha saha
+ 1
Ananiya Jemberu , thank you so much. I appreciate a lot for approaching for help
8th Apr 2021, 7:18 PM
Fardina Kabir
+ 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
8th Apr 2021, 7:51 PM
Ananiya Jemberu
+ 1
Ow, thanks :) Coding Cat [Mouse searching] it just convert single string to char
8th Apr 2021, 8:02 PM
Ananiya Jemberu
+ 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()) }
26th Apr 2021, 1:21 PM
MATHEW Desmond Oyeyemi
+ 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()) }
28th Apr 2021, 8:22 AM
Akash D Dilwale
+ 1
Obi Wan is here to help i have force also
28th May 2021, 1:19 PM
Mackenzie Barr
+ 1
I am the best Skywalker!!!
28th May 2021, 1:23 PM
REY 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") }
8th Sep 2021, 3:44 AM