• Register
1 vote
23 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,240 points

Related questions

0 votes
2 answers 131 views
131 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 4 views
4 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 10.2k points
0 votes
1 answer 16 views
16 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 10.2k points
1 vote
1 answer 27 views
27 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 35 views
35 views
How to get the number from the occurences of individual characters Supose the string is: x = 'cdfghcfonccn' How can i solve this problem.
asked Mar 10, 2020 LizzyM 6.1k points
2 votes
1 answer 29 views
29 views
Problem: Hello guys, I hope you all are doing well. I am learning java programming in my school. My teacher taught us many things about the string in the last class and gave a problem to solve. I tried to solve the problem but couldn&rsquo;t make it happen so far. ... problem I&rsquo;d be grateful to him/her. So, my problem is: find all occurrences of a substring in a string java Thanks, mate.
asked Mar 25, 2020 Gavin 15.3k points
0 votes
1 answer 70 views
70 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
1 vote
1 answer 23 views
23 views
Problem: I tried to count occurrences of individual character of string using python. So I wrote this program for the string: a = 'yanwawny' The program: ​​​string = 'yanwawny' ​dict((letter, string.count(letter)) for letter in set(a) But it throws the error message: File "main.py", line 1 ​a = 'yanwawny' ^ SyntaxError: invalid character in identifier What to do to solve this thing?
asked May 4, 2020 samhaz 5k points
0 votes
1 answer 10 views
10 views
Problem: count occurrences of character in string java using hashmap.
asked Feb 18 Ethan ross 1.9k points
0 votes
1 answer 17 views
17 views
Problem: Hello Kodlogs! I am a beginner and started learning programming through an online course. In course, I am given an assignment in which I have to write a program that will count the occurrence of each character in a string and then print the number of ... with detailed comments so that I can understand the logic and program? The given string is: &ldquo;This is an assignment&rdquo;. Thanks!
asked Jan 6 Code Learner 6.5k points