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

8th Apr 2021, 5:43 PM
Fardina Kabir
Fardina Kabir - avatar
68 Respuestas
+ 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
8th Apr 2021, 7:27 PM
Coding Cat
Coding Cat - avatar
+ 6
//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
Ananiya Jemberu - avatar
8th Apr 2021, 7:28 PM
Fardina Kabir
Fardina Kabir - avatar
+ 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
Coding Cat - avatar
+ 2
Hi
8th Apr 2021, 5:53 PM
Fardina Kabir
Fardina Kabir - avatar
+ 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
Fardina Kabir - avatar
+ 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
Coding Cat - avatar
+ 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
Fardina Kabir - avatar
+ 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
Ananiya Jemberu - avatar
+ 1
Ow, thanks :) Coding Cat [Mouse searching] it just convert single string to char
8th Apr 2021, 8:02 PM
Ananiya Jemberu
Ananiya Jemberu - avatar
+ 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) }
22nd Apr 2021, 10:54 PM
Adrian Java
Adrian Java - avatar
+ 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
MATHEW Desmond Oyeyemi - avatar
+ 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
Akash D Dilwale - avatar
+ 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
Sachin Yadav
Sachin Yadav - avatar
+ 1
Thx Mohamed Yassen
24th Apr 2022, 10:08 PM
Rishabh Sharma
Rishabh Sharma - avatar
+ 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) }
5th Jan 2023, 9:58 PM
Aditya Dixit