Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Search word in given String
- // *case sensitive
- //
- import java.util.Scanner;
- public class nrp5213100087 {
- public static void main (String[] args) {
- search start = new search();
- }
- }
- class search {
- String field;
- String key;
- Scanner get = new Scanner(System.in);
- char[] raw;
- char[] find;
- boolean[] num;
- int lev=-1,rig=-1;
- void finder(int a, int b) { // a=raw b=key
- if(b>=key.length()-1||a>=field.length()-1) {}
- else
- if(raw[a]==find[b]) {
- num[a]=true;
- finder(a+1,b+1);
- }
- else {
- reset();
- finder(a+1,0);
- }
- }
- void fill(String str, char[] arr) {
- for (int i = 0; i < str.length()-1; i++) {
- arr[i]=str.charAt(i);
- }
- }
- void out() {
- for (int i = 0; i < field.length(); i++) {
- if(num[i]==true) {
- lev=1+i;
- rig=1+i+key.length()-1;
- break;
- }
- }
- }
- void reset() {
- for (int i = 0; i < field.length()-1; i++) {
- num[i]=false;
- }
- }
- search() {
- System.out.print("String input : ");
- field = get.nextLine();
- raw = new char[field.length()+1];
- num = new boolean[field.length()+1];
- fill(field, raw);
- reset();
- System.out.print("Keyword : ");
- key = get.nextLine();
- find = new char[key.length()+1];
- fill(key, find);
- finder(0,0);
- out();
- /* debugg
- for (int i = 0; i < field.length(); i++) {
- if(num[i]==true)
- System.out.print("1 ");
- else
- System.out.print("0 ");
- } */
- if(lev!=-1) {
- System.out.printf("\nKata ditemukan\nIndex %d-%d\n",lev,rig);
- }
- else
- System.out.println("\nKata tidak ditemukan\n");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement