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.

advertisement

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

advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification 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
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 20s–40s and exploring new directions in your career, I also offer mentoring. Learn more here.