Printing all characters that appear at least k times in a string | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Printing all characters that appear at least k times in a string

The task is: Find and print in alphabetical order all the characters from the given string, that appear at least k times. I made the code and apparently works with simple examples, but it seems like something is wrong with it and I cannot really find it out. (I really cannot understand why in the last for cycle, if I put i<j-1 I miss the last character, even though the array res has a length of j-1 chars.) if(you_could_help) { printf("I would really appreciate it."); } else { printf("No problem!"); } https://code.sololearn.com/cqairi01v05e/?ref=app

21st Sep 2019, 12:38 PM
Ledio Deda
Ledio Deda - avatar
2 Answers
+ 2
Led10 Деда , it is normal, I tried your code and the variable "j" in fact is the length of the string. So the last char is on position j-1, when you write i < j -1 you say to the program exclude the last char. Hope my explanation could help you 😉
21st Sep 2019, 2:36 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
+ 1
Yes but the thing is that when the last element of res enters, it has the index j-1, and after that the program performs j++, thus j is one bigger than the length of res. I hope u understand what i mean. Moreover there must be sth as the system doesnt accept is as a correct solution(maybe not with the length,but with sth else). However thanks for trying!
21st Sep 2019, 2:49 PM
Ledio Deda
Ledio Deda - avatar