Java Recursion - counting Characters in a string -


i'm trying find number of occurrences "character" found in "str" using recursion. think have concept of reason code not work when test out...do know why wrong?

   public static int countchar(string str, string character) {     int number = 0;     if(str.length()==1) {         return number;     }     if (!(str.substring(0,1).equals(character))) {         return countchar(str.substring(1), character);     } else {         number = number + 1;         return countchar(str.substring(1), character);     } } 

number local variable ....

 public static int countchar(string str, string character) {     if(str.length()==0) {         return 0;     }      if ((str.substring(0,1).equals(character))) {         return 1 + countchar(str.substring(1), character);     }      return countchar(str.substring(1), character); } 

the terminating case when string length zero.

for each step , check current char , if match - add 1 result rest of string, if not return match result rest of string


Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -