• Register
1 vote
30 views

Problem:

Hello All,

I am a novice in Java programming. I am learning Java by taking an online class out of my own curiosity. I am solving basic level problems from many websites. So far, I was doing good. I get stuck as soon as I get a problem that asked for count occurrences of a character in a string. I’ve completed the string topic but the problem is going over my head. I can write a program that counts all the characters in a string but how to solve this particular problem? Do you guys have any idea? Please, help me to solve the problem.

Thanks.

12 7 7
15,250 points

Please log in or register to answer this question.

1 Answer

2 votes

Solution:

Finding occurrences in a string is indeed a good problem to solve. In real-time developing it helps a lot. We can easily find out a particular character’s occurrences in a string using an iteration over the string and inside a method. 

public class MyClass{
    public static int count(String str, char ch){ 
        int result = 0;  
        for (int i=0; i<str.length(); i++){ 
            if (str.charAt(i) == ch) 
            result++; 
        }  
        return result; 
    }      
    public static void main(String args[]){ 
        String str= "I Love Programming"; 
        char ch = 'o'; 
        System.out.println(count(str, ch)); 
    } 
}

Give the above program a try. It should count the occurrences of a particular character. 

Thanks.

13 9 6
94,260 points

Related questions

0 votes
2 answers 7 views
7 views
Problem: I need assistance in resolving this problem of &ldquo;How to find the occurrence of a character in a string in java&rdquo;?
asked Mar 29 tuhin1 48.9k points
0 votes
1 answer 5 views
5 views
Problem: I'll make a submission for a response to my question. Please, I have searched the internet but have not found any useful material, and I am now having trouble continuing my studies.
asked Mar 29 tuhin1 48.9k points
0 votes
1 answer 5 views
5 views
Problem: I am stuck during learning as I have already mentioned in my question someone please cooperate me to continue my project-Thanks
asked Mar 29 tuhin1 48.9k points
0 votes
0 answers 5 views
5 views
Problem: I'm having problems during my learning as I mentioned in my question above, I try to find on web search but I can't get proper information, can you help me to continue my project?
asked Mar 29 tuhin1 48.9k points
0 votes
1 answer 9 views
9 views
Problem: So if my string is "the dude is a cool dude". I'd like to find the first index of 'dude': mystring.findfirstindex('dude') # should return 4 What is the python command for this?
asked Feb 23 Mashhoodch 13k points
0 votes
2 answers 143 views
143 views
Problem : How to count the number of occurrences of a character in a string in java
asked Oct 23, 2019 peterlaw 6.9k points
0 votes
1 answer 69 views
69 views
Problem: Write a C++ program to count the number of occurrences of given number in a sorted array of integers. I have generated an array of 5 random integers from 1-5. Here is what the array looks like now: myArray[5] = {3, 3, 1, 4, 5} I have now sorted the array of 5 integers in ascending order, from least to greatest. myArray[5] = {1, 3, 3, 4, 5}
asked Dec 21, 2020 Mashhoodch 13k points
1 vote
1 answer 31 views
31 views
Problem: Hello Kodlogs, I am a newbie in Python programming. I am learning Python by enrolling in an online course. I am solving entry-level problems from some websites. I was doing well unless I get a question that asked for count occurrences of a particular item in a ... light on this? Please, help me to solve the problem. I am looking forward to having your answers. Thanks a lot in advance.
asked Jul 31, 2020 adamSw 11.3k points
0 votes
1 answer 98 views
98 views
Problem: Hello All, I am a novice in Python programming. I am learning Python by enrolling in an online class out of my own curiosity. I am solving basic level problems from many websites. So far, I was doing good. I get stuck as soon as I get a question that ... you guys have any idea? Please, help me to solve the problem. I am looking forward to having your answers. Thanks a lot in advance.
asked Jul 5, 2020 adamSw 11.3k points
0 votes
1 answer 5 views
5 views
Problem: I&rsquo;m new in java. How do I count the number of occurrences of a char in a String? I have the string a.b.c.d I want to count the occurrences of '.' in an idiomatic way, preferably a one-liner. How to count it? Please help me to learn. Thanks.
asked Mar 15 Sabbir Hossain 14.6k points