Advertisement
cd62131

CountChar

Mar 7th, 2014
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 0.38 KB | None | 0 0
  1. public class CountChar {
  2.   public static int getChar(String str, char chr) {
  3.     int count = 0, pos = 0;
  4.     while (str.indexOf(chr, pos) != -1) {
  5.       count++; pos++;
  6.     }
  7.     return count;
  8.   }
  9.  
  10.   public static void main(String[] args) {
  11.     System.out.println("a in aaaa: " + getChar("aaaa", 'a'));
  12.     System.out.println("b in aaaa: " + getChar("aaaa", 'b'));
  13.   }
  14. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement