-
Notifications
You must be signed in to change notification settings - Fork 0
/
11ABCheck.java
39 lines (32 loc) · 1.09 KB
/
11ABCheck.java
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
/*
* Description: Using the Java language, have the function ABCheck(str) take
* the str parameter being passed and return the string true if the characters
* a and b are separated by exactly 3 places anywhere in the string at least
* once (ie. "lane borrowed" would result in true because there is exactly
* three characters between a and b). Otherwise return the string false.
*/
import java.util.*;
import java.io.*;
class Function {
String ABCheck(String str) {
char[] myArray = str.toLowerCase().toCharArray();
for(int i = 0; i < str.length(); i++) {
if(((i+4 < str.length() && myArray[i+4] == 'b') && myArray[i] == 'a')) {
str = "true";
return str;
}
else if(((i+4 < str.length() && myArray[i+4] == 'a') && myArray[i] == 'b')) {
str = "true";
return str;
}
}
str = "false";
return str;
}
public static void main (String[] args) {
// keep this function call here
Scanner s = new Scanner(System.in);
Function c = new Function();
System.out.print(c.ABCheck(s.nextLine()));
}
}