• Post Reply Bookmark Topic Watch Topic
  • New Topic

Number of given character accurrence  RSS feed

 
Michael Gandia
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have here a code that will count the accurrence of the given character in a string, but my code below just search the first given character like for example I entered AE then it will only search A. Can someone help so that it will also count the second char. Thanks!
input sentence: java sentence
enter the letters: (No spaces) AE

Output:
A=2
E=3
   
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
also count the second char. 

What about something like this with a nested loop:
get sentence to search
get chars to search for
begin outer loop
get next char to search for
begin inner loop
search sentence for that char
end inner loop
print count of times found
end outer loop
 
Campbell Ritchie
Marshal
Posts: 55772
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

There is an alternative approach, viz to create a counting program. There is an example of a counting program in the Java™ Tutorials.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!