In: Computer Science
Input:
StringManipulation.java:
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
public class StringManipulation { //Define a class
    String stringtomatch; //Member variable for storing string for matching
    List<SentenceData> sentences = new ArrayList<>(); //List for storing matched strings
    StringManipulation(String stringtomatch) //Parameterized constructor
    {
        this.stringtomatch = stringtomatch;
    }
    String readFile(String filename) //Read file and return string
    {
        try {
            InputStream is = new FileInputStream(filename);  //Create input stream
            BufferedReader br = new BufferedReader(new InputStreamReader(is)); //Load stream into buffer
            String line = br.readLine(); //Read first line
            StringBuilder sb = new StringBuilder(); //Create a string builder object to store multiple lines into one
            while(line != null){ //Iterate over input text
                sb.append(line).append(" "); //Append each line to string builder
                line = br.readLine();
            }
            return sb.toString(); //Return string
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    void insertString(SentenceData sentence){
        int index = 0; //initialize index
        for(SentenceData data: sentences) //Iterate over list of objects
        {
            if (data.frequence > sentence.frequence) //Check if frequency is higher than new object
            {
                break;
            }
            index++;
        }
        sentences.add(index, sentence); //Insert element at given index
    }
    void matchTokens(String data)
    {
        StringTokenizer tokenizer = new StringTokenizer(data, "."); //Split string by sentences
        while (tokenizer.hasMoreTokens()) //Iterate over list of tokens
        {
            String line = tokenizer.nextToken(); //Store sentence in a string
            if (line.contains(stringtomatch)) //Check if string has matching substring
            {
                //Insert line in the list along with frequency
                insertString(new SentenceData(line, line.split(stringtomatch).length - 1));
            }
        }
    }
    void displayStrings()
    {
        for(SentenceData data: sentences) //Iterate over objects in the list
        {
            //Print string and frequency
            System.out.println("String: "+data.Sentence+"\nFrequency: "+data.frequence);
            System.out.println("-------------------------");
        }
    }
    public static void main(String[] args)
    {
        /*
        Create an object of class and initialize substring
         */
        StringManipulation manip = new StringManipulation("but the");
        String data = manip.readFile("input.txt"); //Fetch file as a single string
        manip.matchTokens(data); //Split string into sentences and store ones that match
        manip.displayStrings(); //Display each string with frequency
    }
    class SentenceData{ //Data class for sentence
        String Sentence;
        int frequence;
        SentenceData(String Sentence, int frequence)
        {
            this.Sentence = Sentence;
            this.frequence = frequence;
        }
    }
}
input.txt:
Darwin was conducting a grave experiment. He had figured out almost all parts of the equation but the only thing yet to be proven was the dual nature of light. He showed his work to scholars but his efforts did not come to fruition. He decided to meet with Ingstad but there was a catch. Ingstad wasn't a gregarious person.
Output:
In Text Format:
String: He had figured out almost all parts of the equation but
the only thing yet to be proven was the dual nature of light
Frequency: 1
-------------------------
String: He decided to meet with Ingstad but there was a catch
Frequency: 1
-------------------------
Brief Explanation: