-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathLinearSearchString.java
More file actions
38 lines (34 loc) · 1.46 KB
/
LinearSearchString.java
File metadata and controls
38 lines (34 loc) · 1.46 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
import java.util.Arrays;
public class LinearSearchSrting {
public static void main(String[] args) {
String name = "Lokesh";
char target = 'L';
System.out.println(linearSearch(name, target));
System.out.println(linearSearch2(name, target));
System.out.println("String as Char array: " + Arrays.toString(name.toCharArray()));
// if Arrays.toString is not used, output will be address of an array : [C@2d98a335
}
static boolean linearSearch(String str , char target){ // we can change the name of target.
if(str.length() == 0){
return false;
} // return false if the string is empty.
for (int i =0; i<str.length(); i++){
if(target == str.charAt(i)){
return true; // return true if target matched with any element in the string str.
}
}
return false; // return false if no any above conditions are satisfied. i.e., not found.
}
static boolean linearSearch2(String str , char target){ // we can change the name of target.
if(str.length() == 0){
return false;
} // return false if the string is empty.
// string.toCharArray() converts string to Character Array.
for (char ch : str.toCharArray()) {
if(ch == target){
return true;
}
}
return false; // return false if no any above conditions are satisfied. i.e., not found.
}
}