Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.65 KB | None | 0 0
  1. /**
  2.  * Created by Mark on 24/02/2017.
  3.     CharArraySearchDemo
  4.  */
  5.  
  6. public class CharArraySearch {
  7.     public static void main(String[] args){
  8.         char[] chars1 = {'h', 'e', 'l','l', 'o'};
  9.         int trump = find01(chars1, 1, 3, 'l');
  10.         int trump1 = find02(chars1, 1, 3, 'l');
  11.         int trump2 = find03(chars1, 1, 3, 'l');
  12.         int trump3 = find04(chars1, 1, 3, 'l');
  13.         System.out.println("The match is found at index " + trump);
  14.         System.out.println("The match is found at index " + trump1);
  15.         System.out.println("The match is found at index " + trump2);
  16.         System.out.println("The match is found at index " + trump3);
  17.  
  18.     }
  19.  
  20.     public static int find01(char[] arr, int start, int end, char target){
  21.         for (;start <= end ; start++){
  22.             if(arr[start] == target) {
  23.                 return start;
  24.             }
  25.         }
  26.         return -1;
  27.     }
  28.  
  29.     public static int find02(char[] arr, int start, int end, char target){
  30.         int index = -1;
  31.         for(;start <= end; start++){
  32.             if(arr[start]== target){
  33.                 index = start;
  34.             }
  35.         }
  36.         return index;
  37.     }
  38.  
  39.     public static int find03(char[] arr, int start, int end, char target){
  40.         for(;end >= start; end--){
  41.             if(arr[end] == target){
  42.                 return end;
  43.             }
  44.         }
  45.         return -1;
  46.     }
  47.  
  48.     public static int find04(char[] arr, int start, int end, char target){
  49.         int index = -1;
  50.         for(;end >= start; end--){
  51.             if(arr[end] == target){
  52.                 index = end;
  53.             }
  54.         }
  55.         return index;
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement