Can anyone please help me with this solving this code in Kotlin. I tried a lot but it's not going well. | Sololearn: Learn to code for FREE!

+10

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

4/8/2021 5:43:11 PM

Fardina Kabir

39 Answers

New Answer

+14

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

Thank you Coding Cat [Mouse searching]

+4

//I guess this will help you fun letterCount(letter: String, text: String): Int { return text.toList().count{ it == letter.single() } }

+3

+2

Ananiya Jemberu , thank you so much. I appreciate a lot for approaching for help

+2

Sorry, didn't read your post right. Have changed my solution to a funktion with return value πŸ˜‰ https://code.sololearn.com/c5JZpc8gg482/?ref=app

+1

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

+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

Ananiya Jemberu nothing! I've tryed your solution. It works fine for me πŸ‘ Didn't know this .single() funktion since now. Very nice πŸ˜€

+1

Ow, thanks :) Coding Cat [Mouse searching] it just convert single string to char

+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

yes, the force is with me.

+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") }

0

Fardina Kabir didn't it work

0

Ananiya Jemberu , I tried it with ur. It didn't work. But now I think it's ok. Because I was able to figure it out

0

Thank you πŸ™

0