(String Search) Algorithm na bincike na kirtani a ciki Java

Algorithm na Binciken Kirtani wata hanya ce ta asali a cikin Java shirye-shiryen da ake amfani da ita don nemo takamaiman kirtani a cikin babban kirtani. Ana amfani da wannan algorithm a cikin aikace-aikacen sarrafa rubutu daban-daban, gami da masu gyara rubutu, injunan bincike, da kayan aikin tantance bayanai.

Yadda Algorithm na Binciken Kirtani ke Aiki

Algorithm na Bincike na String yana aiki ta hanyar kwatanta kowane hali na ƙananan kirtani da ake nema tare da haruffan babban kirtani. Yana jujjuyawa cikin babban kirtani kuma yana bincika yuwuwar wasa ta hanyar kwatanta haruffa ɗaya bayan ɗaya. Idan an sami ashana, yana nuna wurin farawa na babban kirtani.

Abũbuwan amfãni da rashin amfani Algorithm na Binciken Kirtani

Amfani:

  • Sauƙaƙan Aiwatarwa: Algorithm yana da sauƙin fahimta da aiwatarwa, yana mai da shi dacewa da ainihin ayyukan neman kirtani.
  • Wanda ya dace da yanayi daban-daban: Ana iya amfani da wannan algorithm a cikin aikace-aikace da yawa waɗanda suka haɗa da neman takamaiman tsari a cikin bayanan rubutu.

Rashin hasara:

  • Rashin inganci ga Manyan Rubutu: A cikin mafi munin yanayi, rikitarwar lokacin algorithm na iya zama babba, yana sa ya zama mara inganci ga manyan rubutu.
  • Ƙimar Ƙa'idar Ƙimar Ƙimar Ƙimar: Asalin sigar algorithm maiyuwa ba za ta iya ɗaukar ƙayyadaddun buƙatun daidaita tsarin ba.

Misali da Bayani

Bari mu yi la'akari da misali na amfani da Algorithm na Binciken Kirtani don nemo takamaiman kalma a cikin jumla a Java.

public class StringSearchExample {  
    public static int searchString(String mainString, String substring) {  
        int mainLength = mainString.length();  
        int subLength = substring.length();  
  
        for(int i = 0; i <= mainLength- subLength; i++) {  
            int j;  
  
            for(j = 0; j < subLength; j++) {  
                if(mainString.charAt(i + j) != substring.charAt(j)) {  
                    break;  
                }  
            }  
  
            if(j == subLength) {  
                return i; // Substring found at position i  
            }  
        }  
  
        return -1; // Substring not found  
    }  
  
    public static void main(String[] args) {  
        String text = "The quick brown fox jumps over the lazy dog";  
        String search = "fox";  
  
        int position = searchString(text, search);  
  
        if(position != -1) {  
            System.out.println("Substring found at position: " + position);  
        } else {  
            System.out.println("Substring not found");  
        }  
    }  
}  

A cikin wannan misali, algorithm ɗin yana neman ƙaramin igiya "fox" a cikin rubutun da aka bayar. Yana jujjuya kowane harafi na rubutun, yana kwatanta shi da haruffan rubutun. Lokacin da aka sami wasa, algorithm ɗin yana mayar da wurin farawa na ƙaramin kirtani a cikin rubutu.

Wannan yana misalta yadda Algorithm na Binciken Kirtani zai iya gano ƙananan igiyoyi a cikin bayanan rubutu mafi girma, yana mai da shi muhimmiyar dabara don sarrafa rubutu da bincike a cikin Java shirye-shirye.