int j =0; for(int n=0;n<str1.length();n++) { String s=str1.substring(n,n+1); if(s.equals("i")){ j++; } } System.out.println(j);