From 48fb9afce690e74a71299540b54dcd15e1505adc Mon Sep 17 00:00:00 2001 From: Ayush Parikh Date: Wed, 26 May 2021 10:36:06 +0530 Subject: [PATCH 1/3] Create binarysearch.java --- binarysearch.java | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 binarysearch.java diff --git a/binarysearch.java b/binarysearch.java new file mode 100644 index 0000000..421f93b --- /dev/null +++ b/binarysearch.java @@ -0,0 +1,25 @@ +class BinarySearchExample{ + public static void binarySearch(int arr[], int first, int last, int key){ + int mid = (first + last)/2; + while( first <= last ){ + if ( arr[mid] < key ){ + first = mid + 1; + }else if ( arr[mid] == key ){ + System.out.println("Element is found at index: " + mid); + break; + }else{ + last = mid - 1; + } + mid = (first + last)/2; + } + if ( first > last ){ + System.out.println("Element is not found!"); + } + } + public static void main(String args[]){ + int arr[] = {10,20,30,40,50}; + int key = 30; + int last=arr.length-1; + binarySearch(arr,0,last,key); + } +} From 7e7e3015e0390e0c2cf346f9a169b0f61707ab80 Mon Sep 17 00:00:00 2001 From: Ayush Parikh Date: Wed, 26 May 2021 10:49:11 +0530 Subject: [PATCH 2/3] Create findcharinstring.java --- findcharinstring.java | 44 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 findcharinstring.java diff --git a/findcharinstring.java b/findcharinstring.java new file mode 100644 index 0000000..b1f53fa --- /dev/null +++ b/findcharinstring.java @@ -0,0 +1,44 @@ +/*Java program to find the occurrence of a character in a string */ +class JavaExample { + + static void countEachChar(String str) + { + //ASCII values ranges upto 256 + int counter[] = new int[256]; + + //String length + int len = str.length(); + + /* This array holds the occurrence of each char, For example + * ASCII value of A is 65 so if A is found twice then + * counter[65] would have the value 2, here 65 is the ASCII value + * of A + */ + for (int i = 0; i < len; i++) + counter[str.charAt(i)]++; + + // We are creating another array with the size of String + char array[] = new char[str.length()]; + for (int i = 0; i < len; i++) { + array[i] = str.charAt(i); + int flag = 0; + for (int j = 0; j <= i; j++) { + + /* If a char is found in String then set the flag + * so that we can print the occurrence + */ + if (str.charAt(i) == array[j]) + flag++; + } + + if (flag == 1) + System.out.println("Occurrence of char " + str.charAt(i) + + " in the String is:" + counter[str.charAt(i)]); + } + } + public static void main(String[] args) + { + String str = "beginnersbook"; + countEachChar(str); + } +} From 34ebfa5ce81b6e8c1e23bbc7b21a8ed9bb55bccc Mon Sep 17 00:00:00 2001 From: Ayush Parikh Date: Sun, 30 May 2021 09:32:46 +0530 Subject: [PATCH 3/3] Delete binarysearch.java --- binarysearch.java | 25 ------------------------- 1 file changed, 25 deletions(-) delete mode 100644 binarysearch.java diff --git a/binarysearch.java b/binarysearch.java deleted file mode 100644 index 421f93b..0000000 --- a/binarysearch.java +++ /dev/null @@ -1,25 +0,0 @@ -class BinarySearchExample{ - public static void binarySearch(int arr[], int first, int last, int key){ - int mid = (first + last)/2; - while( first <= last ){ - if ( arr[mid] < key ){ - first = mid + 1; - }else if ( arr[mid] == key ){ - System.out.println("Element is found at index: " + mid); - break; - }else{ - last = mid - 1; - } - mid = (first + last)/2; - } - if ( first > last ){ - System.out.println("Element is not found!"); - } - } - public static void main(String args[]){ - int arr[] = {10,20,30,40,50}; - int key = 30; - int last=arr.length-1; - binarySearch(arr,0,last,key); - } -}