问题
This is a question in the openDSA interactive learning platform from Virginia Tech:
For function "countChr", write the missing part of the recursive call. This function should return the number of times that the letter "A" appears in string "str".
int countChr(String str) {
if (str.length() == 0) {
return 0;
}
int count = 0;
if (str.substring(0, 1).equals("A")) {
count = 1;
}
return count + <<Missing a Recursive call>>
}
I know how to find a character non recursively in the following way:
public static void main(String [] args) {
String str ="abdcfghaasdfaadftaxvvaacvbtradcea";
int count =0;
for(int n=0; n<= str.length()-1; n++) {
if(str.charAt(n)== 'a')
count++;
}
System.out.print(count);
}
I really don't know how to do the same recursively, especially following the exact pattern given in the question.
回答1:
To recursively obtain the number of occurrences of the letter 'A', you need to recursively call the function with the substring from index 1 to the end of the string:
public class Example {
public static void main(String [] args) {
String str ="abdcfghaasdfaadftaxvvaacvbtradcea";
System.out.println(countChr(str));
String str2 ="abdcfAhaasdAaadftaxvAAAacvbtradcea";
System.out.println(countChr(str2));
}
static int countChr(String str) {
if (str.length() == 0) {
return 0;
}
int count = 0;
if (str.substring(0, 1).equals("A")) {
count = 1;
}
return count + countChr(str.substring(1));
}
}
Output:
0
5
Explanation of how this works:
- The function is first called with the entire String
- If the String length is 0 return 0 because there cannot be an occurrence of 'A'
- Initialise a counter to 0, which will be used to count the number of occurrences.
- If the first character of the String is 'A' increment the counter
- Now to repeat this process, we need to call the same function with the same String, except without the first character. We add the result of this recursive call to the counter, and return it.
This process can be illustrated by adding some prints:
int countChr(String str) {
System.out.println(str);
if (str.length() == 0) {
System.out.println("String has length 0, returning 0");
return 0;
}
int count = 0;
if (str.substring(0, 1).equals("A")) {
System.out.println("Character is an 'A' adding 1 to the count");
count = 1;
}
return count + countChr(str.substring(1));
}
Output:
abdcfAhaasdAaadftaxvAAAacvbtradcea
bdcfAhaasdAaadftaxvAAAacvbtradcea
dcfAhaasdAaadftaxvAAAacvbtradcea
cfAhaasdAaadftaxvAAAacvbtradcea
fAhaasdAaadftaxvAAAacvbtradcea
AhaasdAaadftaxvAAAacvbtradcea
Character is an 'A' adding 1 to the count
haasdAaadftaxvAAAacvbtradcea
aasdAaadftaxvAAAacvbtradcea
asdAaadftaxvAAAacvbtradcea
sdAaadftaxvAAAacvbtradcea
dAaadftaxvAAAacvbtradcea
AaadftaxvAAAacvbtradcea
Character is an 'A' adding 1 to the count
aadftaxvAAAacvbtradcea
adftaxvAAAacvbtradcea
dftaxvAAAacvbtradcea
ftaxvAAAacvbtradcea
taxvAAAacvbtradcea
axvAAAacvbtradcea
xvAAAacvbtradcea
vAAAacvbtradcea
AAAacvbtradcea
Character is an 'A' adding 1 to the count
AAacvbtradcea
Character is an 'A' adding 1 to the count
Aacvbtradcea
Character is an 'A' adding 1 to the count
acvbtradcea
cvbtradcea
vbtradcea
btradcea
tradcea
radcea
adcea
dcea
cea
ea
a
String has length 0, returning 0
回答2:
You have to call the countChr method again within the method, with the String up to the last character you called. So if you do this:
return count + countChr( str.substring(1) );
That will give you the desired result.
回答3:
return count + countChr(str.substring(1, str.length()));
or a more compact form:
return count + countChr(str.substring(1));
来源:https://stackoverflow.com/questions/37597546/find-a-character-recursively-in-a-string