-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCSc2720_Recursion.java
More file actions
56 lines (42 loc) · 1.51 KB
/
CSc2720_Recursion.java
File metadata and controls
56 lines (42 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
* Program Name: Lab 9 / Recursion
* Name: Stephanie L. Wyckoff
* Date: 3/11/2017
* Course: Data Structures Lab
* Professor: Duan
* TA: Pelin Icer, Shubin Wu
*
* Description: Program takes in a string and checks if the substring "ABBB" is contained within and returns boolean value.
*
* Input: n/a
*
* Output: Statement of whether or not the substring was in the string or not.
*/
public class Recursion {
public static boolean IsIn(String w){
String search = "ABBB"; //string to search for
if(w.length() == 0){ //if string is empty, return true
return true;
}
else if(w.length() != 0){
if(w.contains(search)){ //if string is not empty and contains search string
char[] ch = search.toCharArray(); //convert search string to ch char array
for(int i = 0; i < ch.length; i++){ //iterate through ch char array
String s = String.valueOf(ch); //change element ch[i] to string
w = w.replace(s, ""); //replace part of string w with empty string, effectively removes the search string char by char
}
return IsIn(w); //because search string was found, return recursive method call with newly diminished string
}
}
return false; //if string is not empty but does not contain AnB3n form throughout
}
public static void main(String[] args){
String str = "AABBBBBB";
if(IsIn(str)){
System.out.println("The string " + str + " is in the language.");
}
else{
System.out.println("The string " + str + " is NOT in the language");
}
}
}