21. Program to find vowels and consonants in a String and count them.
public class P21_VowelsAndConsonantsInString {
public static void main(String[] args) {
String inputString = "Miss TecHack";
char[] vowels = new char[5];
char[] consonants = new char[inputString.length()];
int v_flag = 0;
int c_flag = 0;
int v_count = 0;
int c_count = 0;
int index = 0;
System.out.println("For input string '" + inputString + "' -");
while(index < inputString.length()) {
//check vowels
if(inputString.charAt(index) == 'a' || inputString.charAt(index) == 'A'
|| inputString.charAt(index) == 'e' || inputString.charAt(index) == 'E'
|| inputString.charAt(index) == 'i' || inputString.charAt(index) == 'I'
|| inputString.charAt(index) == 'o' || inputString.charAt(index) == 'O'
|| inputString.charAt(index) == 'u' || inputString.charAt(index) == 'U'
) {
//set flag to zero to check for every index
v_flag = 0;
//check if vowel is already present in vowels array and set flag
for(int i = 0; i < vowels.length; i++) {
if(inputString.charAt(index) == vowels[i]) {
v_flag++;
}
}
//if not present in array, add it
//increment count
if(v_flag == 0) {
vowels[v_count] = inputString.charAt(index);
v_count++;
}
}
//check consonants
else if((inputString.charAt(index) >= 'a' && inputString.charAt(index) <= 'z')
||
(inputString.charAt(index) >= 'A' && inputString.charAt(index) <= 'Z')
) {
//set flag to zero to check for every index
c_flag = 0;
//check if consonant is already present in consonants array and set flag
for(int i = 0; i < consonants.length; i++) {
if(inputString.charAt(index) == consonants[i]) {
c_flag++;
}
}
//if not present in array, add it
//increment count
if(c_flag == 0) {
consonants[c_count] = inputString.charAt(index);
c_count++;
}
}
index++;
}
System.out.println("Vowels count : " + v_count + "( " +
String.join(",", String.valueOf(vowels).trim().split("")) + " )");
System.out.println("Consonants count : " + c_count + "( " +
String.join(",", String.valueOf(consonants).trim().split("")) + " )");
}
}
22. Program to find permutations of a String.
public class P22_FindPermutationsOfAString {
public static void main(String[] args) {
findPermutation("","ABC");
}
private static void findPermutation(String perm, String word) {
if (word.isEmpty()) {
System.out.println(perm + word);
} else {
for (int i = 0; i < word.length(); i++) {
//one character fix and then calculating permutations of others
findPermutation(perm + word.charAt(i), word.substring(0, i) + word.substring(i + 1, word.length()));
}
}
}
}
23. Program to read input from the user using the command line.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class P23_ReadUserInput {
public static void main(String[] args) {
ReadUserInput r = new ReadUserInput();
r.scanner();
r.bufferReader();
}
public void scanner() {
Scanner s = new Scanner(System.in);
System.out.println("Enter username");
String userName = s.nextLine();
System.out.println("Username is: " + userName);
}
public void bufferReader() {
try {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
System.out.println("Enter Name");
String name = br.readLine();
System.out.println("Name: " + name);
} catch (IOException e) {
e.printStackTrace();
}
}
}
24. Program to perform a binary search.
public class P24_BinarySearch {
public static void main(String[] args) {
int[] list = {5,23,45,78,25,46,12,32,18,55};
int search = 250;
int index = binarySearch(list, search);
if(index == -1) {
System.out.println("Search key not found in list");
} else {
System.out.println("Search key found in list at index: " + index);
}
}
public static int binarySearch(int[] input, int number) {
int low = 0;
int high = input.length - 1;
while (high >= low) {
int middle = (low + high) / 2;
if (input[middle] == number) {
return middle;
} else if (input[middle] < number) {
low = middle + 1;
} else if (input[middle] > number) {
high = middle - 1;
}
}
return -1;
}
}
25. Program to perform sorting using a bubble sort algorithm.
import java.util.Arrays;
public class P25_BubbleSort {
public static void main(String[] args) {
int[] list = { 5, 23, 45, 78, 25, 46, 12, 32, 18, 55 };
bubbleSort(list);
}
public static void bubbleSort(int[] numbers) {
for (int i = 0; i < numbers.length; i++) {
for (int j = numbers.length - 1; j > i; j--) {
if (numbers[j] < numbers[j - 1]) {
swap(numbers, j, j - 1);
}
}
}
System.out.println("Sorted Array using Bubble sort algorithm " + Arrays.toString(numbers));
}
public static void swap(int[] array, int from, int to) {
int temp = array[from];
array[from] = array[to];
array[to] = temp;
}
}
26. Program to perform sorting using a quick sort algorithm.
import java.util.Arrays;
public class P26_QuickSort {
int[] list = { 5, 23, 45, 78, 25, 46, 12, 32, 18, 55 };
int length = list.length;
public static void main(String[] args) {
QuickSort qs = new QuickSort();
if (qs.list == null || qs.list.length == 0) {
return;
}
qs.quickSort(0, qs.length - 1);
System.out.println("Sorted array :" + Arrays.toString(qs.list));
}
private void quickSort(int low, int high) {
int i = low;
int j = high;
int mid = list[low + (high - low) / 2];
while (i <= j) {
while (list[i] < mid) {
i++;
}
while (list[j] > mid) {
j--;
}
if (i <= j) {
swap(i, j);
i++;
j--;
}
if (low < j) {
quickSort(low, j);
}
if (i < high) {
quickSort(i, high);
}
}
}
private void swap(int i, int j) {
int temp = list[i];
list[i] = list[j];
list[j] = temp;
}
}
27. Program to perform sorting using an insertion sort algorithm.
import java.util.Arrays;
public class P27_InsertionSort {
public static void main(String[] args) {
int[] list = { 5, 23, 45, 78, 25, 46, 12, 32, 18, 55 };
insertionSort(list);
System.out.println("Sorted Array using Insertion sort algorithm : " + Arrays.toString(list));
}
public static void insertionSort(int[] unsortedList) {
for (int i = 1; i < unsortedList.length; i++) {
int current = unsortedList[i];
int j = i;
while (j > 0 && unsortedList[j - 1] > current) {
unsortedList[j] = unsortedList[j - 1];
j--;
}
unsortedList[j] = current;
}
}
}