🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
In this blog post, we will learn how to write a Kotlin program to count the occurrence of a character in a String.
Counting the occurrence of a character in a string is a common programming task. It allows us to determine the frequency of a specific character within a given string. In this blog post, we will explore a Kotlin program that efficiently counts the occurrence of a character in a string. We will walk through the program step by step, explaining the logic behind it.
Counting the occurrence of a character in a string is a common programming task. It allows us to determine the frequency of a specific character within a given string. In this blog post, we will explore a Kotlin program that efficiently counts the occurrence of a character in a string. We will walk through the program step by step, explaining the logic behind it.
Kotlin Program: Counting the Occurrence of a Character in a String
To count the occurrence of a character in a string in Kotlin, we can follow a straightforward approach. Let's dive into the code:
fun countOccurrences(str: String, ch: Char): Int {
var count = 0
for (element in str) {
if (element == ch) {
count++
}
}
return count
}
fun main() {
val inputString = "Hello, World!"
val character = 'o'
val occurrenceCount = countOccurrences(inputString, character)
val character1 = 'l'
val occurrenceCount1 = countOccurrences(inputString, character1)
println("The character '$character' occurs $occurrenceCount times in the string.")
println("The character '$character1' occurs $occurrenceCount1 times in the string.")
}
Output:
The character 'o' occurs 2 times in the string.
The character 'l' occurs 3 times in the string.The countOccurrences() function takes a string str and a character ch as input and returns the count of occurrences of ch in str.
We initialize a variable count to 0 to keep track of the occurrence count.
We iterate through each character element in the str string using a for loop.
Inside the loop, we compare each element with the given character ch. If they are equal, we increment the count by 1.
After iterating through all the characters in the string, we return the final count value.
In the main() function, we create a sample input string (val inputString = "Hello, World!") and specify the character we want to count (val character = 'o').
We call the countOccurrences() function with the input string and character, and the returned occurrence count is stored in the occurrenceCount variable.
Finally, we print the result to the console using println().
Conclusion
In this blog post, we have discussed a Kotlin program that efficiently counts the occurrence of a character in a given string. This program can be used to analyze text data, perform character frequency analysis, or identify patterns within strings.
Feel free to explore and modify the code to suit your specific needs. The concept of counting occurrences is essential in various programming scenarios, such as text processing, data analysis, and algorithmic challenges. Happy coding!
Feel free to explore and modify the code to suit your specific needs. The concept of counting occurrences is essential in various programming scenarios, such as text processing, data analysis, and algorithmic challenges. Happy coding!
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
🆕 High-Demand
80–90% OFF
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
🆕 High-Demand
80–90% OFF
ChatGPT + Generative AI + Prompt Engineering for Beginners
🚀 Trending Now
80–90% OFF
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
🌟 Top Rated
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
🌟 Top Rated
80–90% OFF
Testing Spring Boot Application with JUnit and Mockito
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Master Spring Data JPA with Hibernate
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
🎓 Student Favorite
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Comments
Post a Comment
Leave Comment