递归实现字符或单词在字符串中是否存在

import java.util.*;
public class Dictionary {
    static String dictionary = new String("abskljlsajdfklajflkdhalkldfjalhflas");
    public static void main(String []args){
        Scanner in = new Scanner(System.in);
        String input = in.nextLine().trim();
        System.out.println(indexSearch(input));
    }
    static boolean indexSearch(String str){
        boolean flag = true ;
        if(str.length()>dictionary.length()){
            return false;
        }
        for(int i = 0 ;i < str.length() ;i++){
            if(str.charAt(i)!= dictionary.charAt(i)){
                flag = false ;
                break;
            }
        }
        if (flag == true){
            return true;
        }
        else{
            dictionary = dictionary.substring(1,dictionary.length());
            System.out.println(dictionary);
            return indexSearch(str);
        }
    }
}

 

posted on 2015-10-11 15:41  上发条的姑娘  阅读(215)  评论(0编辑  收藏  举报

导航