0% found this document useful (0 votes)
1 views

Recursion

Bjn

Uploaded by

ymkbthz5yr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Recursion

Bjn

Uploaded by

ymkbthz5yr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Recursion L11 / L12

Course Lecture notes

Date @November 6, 2024

Recursion
Previously we learned how method can call other methods.
However, Recursion method its a method that call itself.
There is two way to use recursion one directly and one with a helper.

Example: prints the numbers from n to 1

//n is already define so n=3

void print(int n){


if(n > 0) {
System.out.print(n+" ");
print(n-1);
}
return;

Recursion L11 / L12 1


}
//output//
3 2 1

💡 The factorial of a number n can be recursively defined as follows:

0! = 1; --> recursive call


n! = n × (n - 1)!; n > 0 --> The base case

💡 The recursive algorithm for computing factorial(n) can be simply


described as follows:

if (n == 0)
return 1;
else
return n * factorial(n - 1);

Similar to loops, recursive methods need a “stopping condition” that


determines when to stop the ‘repetition’.

Recursion can be used to solve For example:


problems that are difficult to ▪ Traversing through file system.
program using simple loops. ▪ Traversing through a tree of search
results.( H tree problems)

//>> Write a "Recursive" program for factorial(4):


Public class Factorial{
public static void main(String[] args){
int f=factorial(4);
System.out.println(f);

Recursion L11 / L12 2


]
//** Recursive Method **//

public. static int factorial (int n){


if(n==0){
return 1; //Stopping condition
}else{
return n * factorial (n-1); //Recursive call
}
}
//output//
24

💡 In the following question we are Invoking factorial(4) recursive calls


to factorial. (Factorial (0) is the base case)

Fibonacci series :

The series: 0 1 1 2 3 5 8 13 21 34 55 89 ...


indexes: 0 1 2 3 4 5 6 7 8 9 10 11

Recursion L11 / L12 3


The Fibonacci series begins with 0 and 1, and each subsequent number is the
sum of the pre-
ceding two
. The series can be recursively defined as:

fib(0) = 0;
fib(1) = 1;
fib(index) = fib(index - 2) + fib(index - 1); index >= 2

//prompts the user to enter an index and computes the Fibon


acci number for that index.

import java.util.Scanner;
public class ComputeFibonacci {

/** Main method */


public static void main(String[] args) {
// Create a Scanner
Scanner input = new Scanner(System.in);
System.out.print("Enter an index for a Fibonacci number:
");
int index = input.nextInt();

// Find and display the Fibonacci number


System.out.println("The Fibonacci number at index " + index
+ " is " + fib(index));

/** The method for finding the Fibonacci number */


public static long fib(long index) {
if (index == 0){ // Base case
return 0;
}else if (index == 1){// Base case
return 1;
}else {// Reduction and recursive calls
return fib(index - 1) + fib(index - 2);
}
}
//output//

Recursion L11 / L12 4


Enter an index for a Fibonacci number: 1
The Fibonacci number at index 1 is 1
------------------------------------------------------
Enter an index for a Fibonacci number: 6
The Fibonacci number at index 6 is 8

Recursive Helper Methods

public class RecursivePalindrome {


public static boolean isPalindrome(String s) {
return isPalindrome(s, 0, s.length() - 1);
}
private static boolean isPalindrome(String s, int low, int
high) {
if (high <= low){ // Base case return true;
return true;
}
else if (s.charAt(low) != s.charAt(high)){ // Base case ret
urn false;
return false;
}
else
return isPalindrome(s, low + 1, high - 1);
}
public static void main(String[] args) {
System.out.println("Is moon a palindrome? " + isPalindrome
("moon"));
}
}

Recursive Selection Sort


/*Recall that it finds the smallest element in the list and
swaps it with the first element. It then finds the smallest

Recursion L11 / L12 5


element remaining and swaps it with the first element in th
e remaining list, and so on until the remaining list contai
ns only a single element.
*/
public class RecursiveSelectionSort {
public static void sort(double[] list) {
sort(list, 0, list.length - 1); // Sort the entire list
}
private static void sort(double[] list, int low, int high)
{ //helper method
if (low < high) {//base case
// Find the smallest number and its index in list[low
.. high]
int indexOfMin = low;
double min = list[low];
for (int i = low + 1; i <= high; i++) {
if (list[i] < min) {
min = list[i];
indexOfMin = i;
}
}
// Swap the smallest in list[low .. high] with list[low]
list[indexOfMin] = list[low];
list[low] = min;
// Sort the remaining list[low+1 .. high]
sort(list, low + 1, high);
}
}
}
//Two overloaded sort method are defined

/*
Find the smallest element in the list and swap it with the
first element
*/
public static void main(String[] args) {
int[] list = { 3, 4, 7, 2, 9 , 1 };
sort(list);

Recursion L11 / L12 6


System.out.println(Arrays.toString(list)); // list should b
e sorted
}
private static void sort(int[] list) {
sort(list, 0, list.length - 1);
}
private static void sort(int[] list, int low, int high) {
if(low<high) {
//find the smallest value in list[low ... high]
int idxMin = low;
int min = list[low];
for(int i = low; i<=high; i++) {
if(list[i] < min) {
min = list[i];
idxMin = i;
}
}
//swap min with list[low]
list[idxMin] = list[low];
list[low] = min;
//recursively sort remaining list[low+1, high]
sort(list, low+1, high);
}//else if (low == high) return;//stopping condition when l
ist has one element only
}

Recursive Binary Search

/*Binary search was introduced in Section 7.10.2. For binary


■ Case 1: If the key is less than the middle element, recursiv
■ Case 2: If the key is equal to the middle element, the searc
■ Case 3: If the key is greater than the middle element, recu
in the second half of the array.
*/
public class RecursiveBinarySearch {
private static int recursiveBinarySearch(int[] list, int key)

Recursion L11 / L12 7


int low = 0;
int high = list.length - 1;
return recursiveBinarySearch(list, key, low, high);
}
private static int recursiveBinarySearch(int[] list, int key,
if (low > high){ // The list has been exhausted without a matc
return -low - 1;

int mid = (low + high) / 2;


if (key < list[mid])
return recursiveBinarySearch(list, key, low, mid - 1);
else if (key == list[mid])
return mid;
else
return recursiveBinarySearch(list, key, mid + 1, high);
}
}

Finding the Directory Size

The size of a directory is the sum of the sizes of all files in


the directory.

/* Gives a program that prompts the user to enter a directory


its size.
*/

import java.io.File;
import java.util.Scanner;

public class DirectorySize {


public static void main(String[] args) {
// Prompt the user to enter a directory or a file
System.out.print("Enter a directory or a file: ");
Scanner input = new Scanner(System.in);

Recursion L11 / L12 8


String directory = input.nextLine();
// Display the size
System.out.println(getSize(new File(directory)) + " bytes")
}
public static long getSize(File file) {
long size = 0; // Store the total size of all files
if (file.isDirectory()) {
File[] files = file.listFiles(); // All files and subdirecto
for (int i = 0; files != null && i < files.length; i++) {
size += getSize(files[i]); // Recursive call
}
}
else { // Base case
size+=file.length();
}
return size;
}
}
//output//
Enter a directory or a file: c:\book
48619631 bytes

CharacteTower of Hanoi

moving a specified number of disks of distinct sizes from


one tower to
another while observing the following rules (Without
breaking any):

1. There are n disks labeled 1, 2, 3, . . . , n and three towers labeled A, B, and


C.

2. No disk can be on top of a smaller disk at any time.

3. All the disks are initially placed on tower A.

Recursion L11 / L12 9


4. Only one disk can be moved at a time, and it must be the smallest disk on a
tower.

/*a program that prompts the user to enter the number of di


sks and invokes
the recursive method moveDisks to display the solution for
moving the disks.
*/

import java.util.Scanner;
public class TowerOfHanoi {
/** Main method */
public static void main(String[] args) {
// Create a Scanner
Scanner input = new Scanner(System.in); System.out.print("E
nter number of disks: ");
int n = input.nextInt();

Recursion L11 / L12 10


// Find the solution recursively
System.out.println("The moves are:"); moveDisks(n, 'A',
'B', 'C');
}
/*The method for finding the solution to move n disks
from fromTower to toTower with auxTower */
public static void moveDisks(int n, char fromTower,char toT
ower, char auxTower) {
if (n == 1) // Stopping condition //base case
System.out.println("Move disk " + n + " from " + fromTower
+ " to " + toTower);

else {
moveDisks(n - 1, fromTower, auxTower, toTower); System.out.
println("Move disk " + n + " from " +
fromTower + " to " + toTower);
moveDisks(n - 1, auxTower, toTower, fromTower);
}
}
}
//output//
Enter number of disks: 4
The moves are:
Move disk 1 from A to C
Move disk 2 from A to B
Move disk 1 from C to B
Move disk 3 from A to C
Move disk 1 from B to A
Move disk 2 from B to C
Move disk 1 from A to C
Move disk 4 from A to B
Move disk 1 from C to B
Move disk 2 from C to A
Move disk 1 from B to A
Move disk 3 from C to B
Move disk 1 from A to C
Move disk 2 from A to B
Move disk 1 from C to B

Recursion L11 / L12 11


Tail Recursion
A tail recursive method is efficient for reducing stack size.

//Tail recursive
public class ComputeFactorialTailRecursion {
/** Return the factorial for a specified number */
public static long factorial(int n) {
return factorial(n, 1); // Call auxiliary method (invoke
}

/** Auxiliary tail-recursive method for factorial */


private static long factorial(int n, int result) { (auxili
if (n == 0)
return result;
else
return factorial(n - 1, n * result); // Recursive call
}
}

Example:
import java.math.BigInteger;
import java.util.Scanner;

import static resources.lib.UnitTest.*;

/**
* *18.1 (Factorial) Using the BigInteger class introduced
in Section 10.9, you can
* find the factorial for a large number (e.g., 100!).
* Implement the factorial method using recursion.
* Write a program that prompts the user to enter an intege
r and displays its factorial.
*/

Recursion L11 / L12 12


public class Exercise18_01 {

static boolean runTest = false; // Change to true to th


e Test instead of user input.

public static void main(String[] args) {


Scanner in = new Scanner(System.in);

if (runTest) {
System.out.println("Successful test indicator:
" + Test.testFactorial());
} else {
System.out.print("\nEnter an integer to find it
s factorial: ");
long numToFactor = in.nextLong();

System.out.println(numToFactor + "! is " + find


Factorial(numToFactor));
}
}

static BigInteger findFactorial(long num) {


long startIterateNum = 1;
BigInteger initResult = BigInteger.ONE;

return factorial(num, startIterateNum, initResult);


}

static BigInteger factorial(long num, long i, BigIntege


r result) {
if (i <= num) {
result = result.multiply(new BigInteger(String.
valueOf(i)));

return factorial(num, ++i, result);


}

Recursion L11 / L12 13


return result;

private static class Test {

public static boolean testFactorial() {


BigInteger correctResult = new BigInteger("3041
40932017133780436126081660647688443776415689605120000000000
00");
BigInteger res = Exercise18_01.findFactorial(5
0);
try {
assertEquals(correctResult, res);
} catch (AssertionError error) {
error.printStackTrace();
return false;
}
return true;

}
}

/**
* 18.5 (Sum series) Write a recursive method to compute the fo
* m(i) = 1/3 + 2/5 + 3/7 + 4/9 + 5/11 + 6/13 + ...... + i/2i +
* <p>
* Write a test program that displays m(i) for i = 1, 2, . . .
*
* @author Harry D.
*/
public class Exercise18_05 {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {

Recursion L11 / L12 14


System.out.printf("\nm(" + i + ") = %.4f", m(i));
}

private static double m(int target) {


int numerator = 1;
double sum = numerator / ((2.0 * numerator) + 1);
return m(sum, numerator, target);
}

private static double m(double sum, int numerator, int target


if (numerator == target) {
return sum;
}
numerator++;
sum += numerator / ((2.0 * numerator) + 1);

return m(sum, numerator, target);


}
}

import java.util.Scanner;

/**
* *18.10 (Occurrences of a specified character in a strin
g) Write a recursive method that
* finds the number of occurrences of a specified letter in
a string using the following method header:
* public static int count(String str, char a)
* <p>
* For example, count("Welcome", 'e') returns 2.
* <p>
* Write a test program that prompts the user to enter a st
ring and a character, and displays the number of
* occurrences for the character in the string.
*/
public class Exercise18_10 {

Recursion L11 / L12 15


static int count;

public static void main(String[] args) {


Scanner in = new Scanner(System.in);
System.out.print("Enter a String and a character to
count it's occurrences:");
String str = in.next();
char ch = in.next().charAt(0);

System.out.println("Character " + ch + " occurs " +


count(str, ch) + " times in " + str);
in.close();
}

public static int count(String str, char a) {


if (str.length() > 0) {
if (str.charAt(str.length() - 1) == a) {
return 1 + count(str.substring(0, str.lengt
h() - 1), a);
} else {
return count(str.substring(0, str.length()
- 1), a);
}
}
return 0;

}
}

import java.util.Scanner;

/**
* *18.17 (Occurrences of a specified character in an arra
y) Write a recursive method that
* finds the number of occurrences of a specified character
in an array. You need to
* define the following two methods. The second one is a re
cursive helper method.

Recursion L11 / L12 16


* public static int count(char[] chars, char ch)
* public static int count(char[] chars, char ch, int high)
* Write a test program that prompts the user to enter a li
st of characters in one line,
* and a character, and displays the number of occurrences
of the character in the list.
*/
public class Exercise18_17 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a list of characters in one
line: ");
String line = in.nextLine();
char[] chars = line.toCharArray();

System.out.println("Enter a single character: ");


char ch = in.next().charAt(0);
System.out.println("The character " + ch + " occurs
" + count(chars, ch) + " times.");
in.close();
}

public static int count(char[] chars, char ch) {


return count(chars, ch, chars.length - 1);
}

public static int count(char[] chars, char ch, int hig


h) {
if (high > 0) {
return chars[high] == ch ? (1 + count(chars, c
h, high - 1)) : (count(chars, ch, high - 1));
} else {
return 0;
}
}
}

Recursion L11 / L12 17


import java.util.Scanner;

/**
* *18.11 (Sum the digits in an integer using recursion) Wr
ite a recursive method that
* computes the sum of the digits in an integer. Use the fo
llowing method header:
* public static int sumDigits(long n)
* For example, sumDigits(234) returns 2 + 3 + 4 = 9.
* <p>
* Write a test program that prompts the user to enter an i
nteger and displays its sum.
* Section 18.5
*/
public class Exercise18_11 {
static int sum;

public static void main(String[] args) {


Scanner in = new Scanner(System.in);
System.out.println("Enter an integer: ");
long num = in.nextInt();
System.out.println("Sum of the digits in " + num +
" is " + sumDigits(num));

public static int sumDigits(long n) {


if (n == 0) {
return sum;
}
long i = n % 10;
sum += i;
n /= 10;
return sumDigits(n);
}

Recursion L11 / L12 18


import java.util.Scanner;

/**
* **18.12 (Print the characters in a string reversely) Rew
rite Programming Exercise 18.9
* using a helper method to pass the substring high index t
o the method. The
* helper method header is:
* public static void reverseDisplay(String value, int hig
h)
*/
public class Exercise18_12 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a String: ");
String s = in.next().trim();
reverseDisplay(s, s.length() - 1);
}

public static void reverseDisplay(String value, int hig


h) {
if (!(value.length() == 0)) {
System.out.print(value.charAt(high));
value = value.substring(0, high);
reverseDisplay(value, value.length() - 1);
}
}
}

import java.util.Scanner;

/**
* *18.13 (Find the largest number in an array) Write a rec
ursive method that returns the
* largest integer in an array.
* <p>
* Write a test program that prompts the user to enter a li

Recursion L11 / L12 19


st of eight integers and displays the largest element.
*/
public class Exercise18_13 {
static int currIdx = 0;

public static void main(String[] args) {


int[] arr = new int[8];
Scanner in = new Scanner(System.in);
System.out.println("Enter a list of eight integers:
");
for (int i = 0; i < 8; i++) {
arr[i] = in.nextInt();
}
in.close();
System.out.println("The largest integer in the list
is " + largestInteger(Integer.MIN_VALUE, arr));

static int largestInteger(int large, int[] integers) {


if (currIdx == integers.length) {
return large;
}
large = Math.max(large, integers[currIdx++]);
return largestInteger(large, integers);
}
}

import java.util.Scanner;

/**
* *18.14 (Find the number of uppercase letters in a strin
g)
* Write a recursive method to return the number of upperca
se letters in a string.
* <p>
* Write a test program that prompts the user to enter a st

Recursion L11 / L12 20


ring and displays the number of uppercase letters in
* the string.
*/
public class Exercise18_14 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a String: ");
String s = in.nextLine().trim();
System.out.println("The number of uppercase letters
in " + s + " is: " + numUpperCase(s, 0, 0));

static int numUpperCase(String str, int idx, int count)


{
if (idx == str.length()) {
return count;
}
if (Character.isUpperCase(str.charAt(idx))) {
count++;
}
return numUpperCase(str, ++idx, count);
}
}

import java.util.Scanner;

/**
* *18.16 (Find the number of uppercase letters in an arra
y) Write a recursive method
* to return the number of uppercase letters in an array of
characters. You need to
* define the following two methods. The second one is a re
cursive helper method.
* public static int count(char[] chars)
* public static int count(char[] chars, int high)
* Write a test program that prompts the user to enter a li
st of characters in one line

Recursion L11 / L12 21


* and displays the number of uppercase letters in the lis
t.
*/
public class Exercise18_16 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a list of characters in one
line: ");

String line = in.nextLine();


char[] chars = line.toCharArray();

System.out.println("The number of uppercase letters


in the list is: " + count(chars));
in.close();
}

public static int count(char[] chars) {


return count(chars, chars.length - 1);
}

public static int count(char[] chars, int high) {


if (high > 0) {
return Character.isUpperCase(chars[high]) ? (1
+ count(chars, high - 1)) : (count(chars, high - 1));
} else {
return 0;
}

}
}

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.StrokeType;

Recursion L11 / L12 22


import javafx.stage.Stage;

/**
* 18.20 (Display circles) Write a Java program that displa
ys ovals,
* as shown in Figure 18.12b.
* <p>
* The circles are centered in the pane.
* <p>
* The gap between two adjacent circles is 10 pixels,
* and the gap between the border of the pane and the large
st
* circle is also 10.
*/
public class Exercise18_20 extends Application {

@Override
public void start(Stage primaryStage) {
RecursiveCirclePane circlePane = new RecursiveCircl
ePane();
Scene scene = new Scene(circlePane, 340, 340);
circlePane.widthProperty().addListener(cl -> circle
Pane.resetCircles());
circlePane.heightProperty().addListener(cl -> circl
ePane.resetCircles());
primaryStage.setScene(scene);
primaryStage.show();

circlePane.paint();

static class RecursiveCirclePane extends Pane {


private final double distanceBetween = 10.0;

RecursiveCirclePane() {

Recursion L11 / L12 23


private void resetCircles() {
this.getChildren().clear();
paint();
}

protected void paint() {


this.setCenterShape(true);
double initialRadius = (((getWidth() + getHeigh
t()) / 2) / 2) - distanceBetween;
drawCircles(initialRadius);

protected void drawCircles(double radius) {


if (radius > 9.0) {
Circle circle = new Circle(getWidth() / 2,
getHeight() / 2, radius);
circle.setStroke(Color.BLACK);
circle.setFill(Color.TRANSPARENT);
this.getChildren().add(circle);
drawCircles(radius - distanceBetween);
}
}
}

import java.util.Scanner;

/**
* 18.21 (Decimal to binary) Write a recursive method that
converts a decimal number
* into a binary number as a string. The method header is:
* public static String dec2Bin(int value)
* Write a test program that prompts the user to enter a de
cimal number and displays its binary equivalent.

Recursion L11 / L12 24


*/
public class Exercise18_21 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a decimal(base 10) number a
s an integer: ");
int decValue = in.nextInt();
System.out.println(decValue + " converted to binary
is: " + dec2Bin(decValue));

in.close();
}

public static String dec2Bin(int value) {


return dec2Bin(value, "");
}

static String dec2Bin(int value, String binary) {


if (value > 0) {
return dec2Bin(value / 2, (value % 2) + binar
y);
}
return binary;
}
}

import java.util.Scanner;

/**
* 18.22 (Decimal to hex) Write a recursive method that con
verts a decimal number into
* a hex number as a string. The method header is:
* public static String dec2Hex(int value)
* <p>
* <p>
* Write a test program that prompts the user to enter a de
cimal number and displays its hex equivalent

Recursion L11 / L12 25


*/
public class Exercise18_22 {
static String hexValue = "";

public static void main(String[] args) {


Scanner in = new Scanner(System.in);
System.out.print("Enter a decimal number to convert
to hex: ");
int number = in.nextInt();

System.out.println(number + " is equivalent to hexa


decimal number " + dec2Hex(number));
in.close();
}

public static String dec2Hex(int value) {


if (value < 16) {
return getAsHexNumber(value) + hexValue;
}
hexValue = getAsHexNumber(value % 16) + hexValue;
return dec2Hex(value / 16);

static String getAsHexNumber(int value) {


switch (value) {
case 10:
return "A";
case 11:
return "B";
case 12:
return "C";
case 13:
return "D";
case 14:
return "E";
case 15:
return "F";

Recursion L11 / L12 26


default:
return String.valueOf(value);
}

}
}

import java.io.File;
import java.util.Scanner;

/**
* 18.29 (Number of files in a directory) Write a program t
hat prompts the user to enter a
* directory and displays the number of the files in the di
rectory.
*/
public class Exercise18_29 {

public static void main(String[] args) {


System.out.print("Enter a directory: ");
Scanner input = new Scanner(System.in);
String directory = input.nextLine().trim();
File file = new File(directory);
if (file.isDirectory()) {
System.out.println("The directory: " + file.get
Name() + " contains " + getFileCount(file) +
" files.");
} else {
System.out.println("Please ensue you are enteri
ng a directory.");
}
}

public static long getFileCount(File file) {


long TOTAL_FILES = 0;
File[] files = file.listFiles();
if (files != null && files.length > 0) {

Recursion L11 / L12 27


for (File f : files) {
if (f.isFile()) {
TOTAL_FILES += 1;
}

}
}
return TOTAL_FILES;
}
}

import java.io.*;
/**
* 18.30 (Find words in all the files) Write a program that
finds all occurrences of a word in all the files
* under a directory, recursively.
* Pass the parameters from the command line as follows:
* <p>
* java Exercise18_30 dirName word
* For Example:
* cmd:>> javac Exercise18_30.java
* >>>>>> java Exercise18_30 ch_14 static
*/
public class Exercise18_30 {
static int occurrences = 0;

public static void main(String[] args) {


if (args.length < 2) {
System.err.println("Usage: java Exercise18_30 d
irName word");
System.exit(0);
}

String directory = args[0];


String word = args[1];
File dir = new File(directory);

if (dir.isDirectory()) {

Recursion L11 / L12 28


File[] files = dir.listFiles();
if (files != null && files.length > 0) {
try {
wordSearch(files, word, 0);
} catch (IOException ioException) {
ioException.printStackTrace();
}
}

} else {
System.out.println("Please specify a Directory
for 'dirName'. ");
}
System.out.println("The word: \"" + word + "\" occu
rs " + occurrences + " times in: " + dir.getName());

static void wordSearch(File[] files, String word, int f


ileIndex) throws IOException {
/* Recursion Stopping condition */
if (files.length == fileIndex) {
return;
}

if (files[fileIndex].isFile()) {
FileReader fileReader = new FileReader(files[fi
leIndex]);
BufferedReader bufferedReader = new BufferedRea
der(fileReader);
String line;
while ((line = bufferedReader.readLine()) != nu
ll) {
if (line.contains(word)) {
occurrences++;
}
}
}

Recursion L11 / L12 29


wordSearch(files, word, fileIndex + 1);
}

Recursion L11 / L12 30

You might also like