如何使用递归方法检查字符串?

时间:2018-04-27 13:44:12

标签: java recursion

我的任务是使用递归方法来计算工作实例" chicken"在一个字符串中,删除字符串中的任何实例并再次检查,以" chicken"的数量的输出结束。除去。我使用嵌套循环而不是递归更容易,这是我的代码。

package recursive_labs;
import java.util.Scanner;
public class ChickenAnhilator {
public static String Test1;
public static int Checker(String Test1) {
String chicken = "chicken";
int i = 0;

int c = 0;
int z = 0;
if (Test1.length()<7){
    return z;
}
    for(i=0;i<Test1.length();i++) {
        if (true==(chicken==Test1.substring(i, i+6))){
            c++;
            Test1.replace("chicken","");
            i=0;
        }
    }
    return c;
}

//chicken
public static void main(String [] args) {
Scanner Keyboard = new Scanner(System.in);
System.out.println("Enter your string");
Test1 = Keyboard.next();
System.out.print(Checker(Test1));
}
}

运行此项并输入字符串&#34; chicken&#34;我收到这些错误

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 8
at java.lang.String.substring(Unknown Source)
at recursive_labs.ChickenAnhilator.Checker(ChickenAnhilator.java:15)
at recursive_labs.ChickenAnhilator.main(ChickenAnhilator.java:29)

我正在使用eclipse,并想知道如何修复此错误,如果我可以在不使用递归方法的情况下完成此任务。谢谢!

3 个答案:

答案 0 :(得分:1)

您询问是否有办法在不使用复发的情况下实现此目的。  使用String#replace

可以轻松完成此任务
static final String CHICKEN = "chicken";
String input = "I like eating chicken and only a chicken would not eat it.";
int length = input.length();
input = input.replace(CHICKEN, "");
int numOccurrences = (length - input.length()) / CHICKEN.length();

Demo

这里可能值得解释的唯一技巧是如何计算出现次数。我们可以比较替换前后输入字符串的长度,然后将该差异除以chicken的长度。

编辑:您已经指出了一个边缘情况,通过删除chicken实际上留下了更多出现鸡的修改输入。在这种情况下,您可以循环迭代,替换直到输入的大小不变:

static final String CHICKEN = "chicken";
String input = "I like eating chichickencken and only a chicken would not eat it.";
int numOccurrences = 0;
while (input.length() > 0) {
    int currLength = input.length();
    input = input.replace(CHICKEN, "");
    if (input.length() == currLength) break;
    numOccurrences += (currLength - input.length()) / CHICKEN.length();
}

Demo

答案 1 :(得分:0)

递归有两种情况:一种是终端案例,另一种是自我调用案例。

因此,在伪代码中,递归算法看起来像

int countChicken(String input) {
   if no "chicken" in input { return 0; }  // The terminal step.
   // chicken is here.
   int position_after = (end of position of "chicken" in string) + 1;
   String rest_of_string = input.substringAfter(position_after);
   return 1 + countChicken(rest_of_string); // The recursive step.
}

答案 2 :(得分:0)

...只是为了计算鸡肉的乐趣,我的版本看起来像这样:

import java.util.*;
import java.lang.*;

class ChickenCounter {

    private static final String CHICKEN = "chicken";

    public static void main(String args[]) {
        ChickenCounter e = new ChickenCounter();
        Arrays.stream(new String[] {
                "chicken",
                "chicken chicken",
                "cchickenhicken",
                "I like eating chicken and only a chicken would not eat it."
        }).forEach(s -> System.out.println("'" + s + "' has " + e.count(s) + " chicken"));
    }

    private int count(String string) {
        if (!string.contains(CHICKEN)) {
            return 0;
        }
        return 1 + count(string.replaceFirst(CHICKEN, ""));
    }
}