Saturday, December 7, 2013

Java Program to seach a substring in a string

import java.io.*;

public class SearchKeyWord {

    public static void main(String[] args) throws IOException {

        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

        System.out.print("\nPlease enter product Name  :");

        int count = 0;

        boolean flag = false;

        String productName = reader.readLine();

        System.out.print("\nPlease enter Key word:");

        String keyword = reader.readLine();

        for (int index1 = 0; index1 < productName.length(); index1++) {

            for (int index2 = 0, index3 = index1; index2 < keyword.length(); index2++, index3++) {

                if (index3 < productName.length()) {

                    if (productName.charAt(index3) == keyword.charAt(index2)) {

                        flag = true;

                        count++;

                    }

                }

            }

            if (count == keyword.length()) {

                flag = true;

                break;

            }

            else {

                flag = false;

            }

            count = 0;

        }

        if (flag == true) {

            System.out.print("Yes, the key word is present in the product");

        }

        else {

            System.out.print("No, The key word is NOT present in the product");

        }

    }

}

No comments:

Post a Comment