+ 1

What do y'all think about it? Can you do better? Method to count the number of xs in a string (can work with any other strings)

int countXX(String str) { if(str.length()<=0)return 0; char[]count=str.toCharArray(); int index=0; for(int i=0;i<count.length;i++) { if(count[i]=='x') { index++;} } if(index==0)return 0; return index; }

13th Apr 2020, 7:56 PM
David Bukedi Diela
David Bukedi Diela - avatar
2 Respuestas
+ 2
Could be simplified to: int countXX(String str) { int index=0; for(int i=0;i<str.length();i++) if(str.charAt(i)=='x') index++; return index; }
14th Apr 2020, 3:27 AM
John Wells
John Wells - avatar
+ 2
John Wells cool thanks a lot !
14th Apr 2020, 1:43 PM
David Bukedi Diela
David Bukedi Diela - avatar