Skip to content

Commit b0c43ce

Browse files
committed
Example of Search a Char in a String
1 parent ce6b434 commit b0c43ce

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

‎src/LinearSearch/LinearSearch.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ public static void main(String[] args) {
99
System.out.println(answer);
1010
}
1111

12+
//Search in the Array: Return Element
13+
1214
//Search in the Array: Return the index if item found. Return -1 is item not found
1315
static int linearSearch(int[] array, int integer){
1416
if (array.length == 0){
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package LinearSearch;
2+
3+
public class SearchInStrings {
4+
public static void main(String[] args) {
5+
6+
String name = "Pradeep";
7+
char target = 'P';
8+
System.out.println(searchInString(name,target));
9+
}
10+
11+
static boolean searchInString(String name, char target){
12+
13+
if (name.length() == 0){
14+
return false;
15+
}
16+
for (int i = 0; i < name.length(); i++){
17+
if (target == name.charAt(i)){
18+
return true;
19+
}
20+
}
21+
return false;
22+
}
23+
}

0 commit comments

Comments
 (0)