Java Program to Perform String Matching Using Vectors

«
»
This is a java program to perform search using Vectors.

Here is the source code of the Java Program to Implement String Matching Using Vectors. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1.  
  2. package com.sanfoundry.setandstring;
  3.  
  4. import java.util.Scanner;
  5. import java.util.Vector;
  6.  
  7. public class StringSearchUsingVectors
  8. {
  9.     public static void main(String[] args)
  10.     {
  11.         Scanner sc = new Scanner(System.in);
  12.         System.out.println("Enter the main string: ");
  13.         Vector<String> text = new Vector<String>();
  14.         text.add(sc.nextLine());
  15.         System.out.println("Enter the pattern string: ");
  16.         Vector<String> pattern = new Vector<String>();
  17.         pattern.add(sc.nextLine());
  18.         for (int i = 0; i <= text.elementAt(0).length()
  19.                 - pattern.elementAt(0).length(); i++)
  20.         {
  21.             if (text.elementAt(0)
  22.                     .substring(i, i + pattern.elementAt(0).length())
  23.                     .equalsIgnoreCase(pattern.elementAt(0)))
  24.             {
  25.                 System.out.println(pattern.elementAt(0)
  26.                         + " is substring of main string, match found at: "
  27.                         + ++i);
  28.             }
  29.         }
  30.         sc.close();
  31.     }
  32. }

Output:

$ javac StringSearchUsingVectors.java
$ java StringSearchUsingVectors
 
Enter the main string: 
Java Program to Implement String Matching Using Vectors
Enter the pattern string: 
Vectors
Vectors is substring of main string, match found at: 49

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

Note: Join free Sanfoundry classes at Telegram or Youtube
advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.