0% found this document useful (0 votes)
2 views1 page

Java Recursion: Find First & Last Index

The document contains a Java class that defines a recursive method to find the first and last occurrence of a specified character in a given string. The method updates two static variables, 'first' and 'last', to store the indices of these occurrences. The main method demonstrates this functionality by searching for the character 'b' in the string 'brtafghjhbfgh'.

Uploaded by

filmtwotwo
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)
2 views1 page

Java Recursion: Find First & Last Index

The document contains a Java class that defines a recursive method to find the first and last occurrence of a specified character in a given string. The method updates two static variables, 'first' and 'last', to store the indices of these occurrences. The main method demonstrates this functionality by searching for the character 'b' in the string 'brtafghjhbfgh'.

Uploaded by

filmtwotwo
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

public class recurfirstlastt {

public static int first = -1;


public static int last = -1;

public static void recurfst(String name, int idx, char element) {


// base case
if (idx == [Link]()) {
return;
}

// check for 'a'


if ([Link](idx) == element) {
if (first == -1) {
first =last= idx;
} else {
last = idx;
}
}

// recursive call
recurfst(name, idx + 1, element);
}

public static void main(String[] args) {


String name = "brtafghjhbfgh";
recurfst(name, 0, 'b');
[Link]("first at: " + first + " last at: " + last);
}
}

You might also like