Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 12

//1.

extract vowels from string

public class ExtractVowels {


public static String extractVowels(String input) {
StringBuilder vowels = new StringBuilder();

for (char c : input.toCharArray()) {


if (isVowel(c)) {
vowels.append(c);
}
}

return vowels.toString();
}

public static boolean isVowel(char c) {


c = Character.toLowerCase(c);
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
}

public static void main(String[] args) {


String input = "Hello, how are you?";
String extractedVowels = extractVowels(input);
System.out.println("Extracted Vowels: " + extractedVowels); // Output:
eooaeou
}
}

2.Given a string. Count the number of Camel Case characters in it.

Camel case is a naming convention where each word in a compound word is capitalized
except for the first word. To count the number of Camel Case characters in a given
string, you can iterate through the characters and count the uppercase letters that
are not at the beginning of the string. Here's a Java program that does that:

public class CamelCaseCount {


public static int countCamelCase(String input) {
int count = 0;

// Check if the first character is uppercase


if (Character.isUpperCase(input.charAt(0))) {
count++;
}

// Count uppercase characters after the first character


for (int i = 1; i < input.length(); i++) {
if (Character.isUpperCase(input.charAt(i))) {
count++;
}
}

return count;
}

public static void main(String[] args) {


String input = "camelCaseExample";
int camelCaseCount = countCamelCase(input);
System.out.println("Camel Case Count: " + camelCaseCount); // Output: 3
}
}

3. Reverse String: Write a Java program to reverse a given string without using any
library functions. For example, if the input is "hello", the output should be
"olleh".

public class ReverseString {


public static String reverse(String input) {
char[] charArray = input.toCharArray();
int left = 0;
int right = charArray.length - 1;

while (left < right) {


// Swap characters at left and right indices
char temp = charArray[left];
charArray[left] = charArray[right];
charArray[right] = temp;

// Move pointers towards each other


left++;
right--;
}

return new String(charArray);


}

public static void main(String[] args) {


String input = "hello";
String reversed = reverse(input);
System.out.println("Reversed String: " + reversed); // Output: "olleh"
}
}

4. Remove Duplicates: Write a program that removes duplicates from an array of


integers in-place and returns the new length of the array.

public class RemoveDuplicates {


public static int removeDuplicates(int[] nums) {
if (nums.length == 0) {
return 0;
}

int uniqueIndex = 0;

for (int i = 1; i < nums.length; i++) {


if (nums[i] != nums[uniqueIndex]) {
uniqueIndex++;
nums[uniqueIndex] = nums[i];
}
}

return uniqueIndex + 1;
}

public static void main(String[] args) {


int[] nums = {1, 1, 2, 2, 2, 3, 4, 4, 5};
int newLength = removeDuplicates(nums);
System.out.print("Array after removing duplicates: ");
for (int i = 0; i < newLength; i++) {
System.out.print(nums[i] + " ");
}
System.out.println("\nNew Length: " + newLength);
}
}

5. Here's a Java program that counts the frequency of each character in a given
string using arrays and displays the result:

public class CharacterFrequency {


public static void countFrequency(String input) {
int[] frequencyArray = new int[128]; // Assuming ASCII characters

for (char c : input.toCharArray()) {


frequencyArray[c]++;
}

System.out.println("Character Frequency:");
for (int i = 0; i < 128; i++) {
if (frequencyArray[i] > 0) {
System.out.println("'" + (char) i + "': " + frequencyArray[i]);
}
}
}

public static void main(String[] args) {


String input = "hello world";
countFrequency(input);
}
}

6.

public class WordCounter {


private String[] words;
private int[] counts;

public WordCounter(String[] words) {


this.words = words;
this.counts = new int[words.length];
}

public int getCount(String word) {


for (int i = 0; i < words.length; i++) {
if (words[i].equals(word)) {
return counts[i];
}
}
return -1;
}

public void log(String word) {


for (int i = 0; i < words.length; i++) {
if (words[i].equals(word)) {
counts[i]++;
break;
}
}
}

public static void main(String[] args) {


String[] targetWords = {"one", "two", "three"};
WordCounter wc = new WordCounter(targetWords);
wc.log("one");
wc.log("one");
wc.log("two");
wc.log("four");
System.out.printf("%d, %d, %d.", wc.getCount("one"), wc.getCount("two"),
wc.getCount("three"));
}
}

7. a program that checks the number of occurrences in each digit in a series of


string in java using java arrays

public class DigitOccurrencesCounter {


public static void main(String[] args) {
String[] inputStrings = {"abc123", "456xyz", "7890"};

int[] digitOccurrences = countDigitOccurrences(inputStrings);

for (int i = 0; i < digitOccurrences.length; i++) {


System.out.println("Digit " + i + ": " + digitOccurrences[i] + "
occurrences");
}
}

public static int[] countDigitOccurrences(String[] strings) {


int[] digitOccurrences = new int[10]; // One element for each digit (0 to
9)

for (String string : strings) {


for (char c : string.toCharArray()) {
if (Character.isDigit(c)) {
int digit = Character.getNumericValue(c);
digitOccurrences[digit]++;
}
}
}

return digitOccurrences;
}
}

8. Given a string str of lowercase alphabets. The task is to find the maximum
occurring character in the string str. If more than one character occurs the
maximum number of time then print the lexicographically smaller character.

public class MaxOccurringChar {


public static void main(String[] args) {
String str = "abbccccddeeeee";

int[] charCount = new int[26]; // Assuming only lowercase alphabets


// Step 1: Count the occurrences of each character
for (char c : str.toCharArray()) {
charCount[c - 'a']++;
}

char maxChar = 'a'; // Initialize the maximum character to 'a'


int maxCount = 0; // Initialize the maximum count to 0

// Step 2: Find the maximum occurring character


for (int i = 0; i < 26; i++) {
if (charCount[i] > maxCount) {
maxCount = charCount[i];
maxChar = (char) ('a' + i);
}
}

System.out.println("Max Occurring Character: " + maxChar);


}
}

9. // Java program to perform the searching


// operation on a string array

public class GFG {


public static void main(String[] args)
{
String[] arr = { "Apple", "Banana", "Orange" };
String key = "Banana";
boolean flag = false;

for (int i = 0; i < arr.length; i++) {


if (arr[i] == key) {
System.out.println("Available at index "
+ i);
flag = true;
}
}
if (flag == false) {
System.out.println("Not found");
}
}
}

Output
Available at index 1

10. // Java program to demonstrate the various


// methods to iterate over a string array

public class GFG {


public static void main(String[] args)
{
String[] arr = { "Apple", "Banana", "Orange" };

// First method
for (String i : arr) {
System.out.print(i + " ");
}
System.out.println();

// Second method
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();

// Third method
int i = 0;
while (i < arr.length) {
System.out.print(arr[i] + " ");
i++;
}
System.out.println();
}
}

Output
Apple Banana Orange
Apple Banana Orange
Apple Banana Orange

11. // Java program to demonstrate the


// conversion of String array to String

import java.util.Arrays;

class GFG {
public static void main(String[] args)
{
String[] arr
= { "The", "quick", "brown", "fox", "jumps",
"over", "the", "lazy", "dog" };

// converting to string
String s = Arrays.toString(arr);
System.out.println(s);
}
}

Output
[The, quick, brown, fox, jumps, over, the, lazy, dog]

12. // Java program to perform the sorting


// operation on a string array

Sorting:
Sorting of String array means to sort the elements in ascending or descending
lexicographic order.

We can use the built-in sort() method to do so and we can also write our own
sorting algorithm from scratch but for the simplicity of this article, we are using
the built-in method.

import java.util.Arrays;
class GFG {
public static void main(String[] args)
{
String[] arr = { "Apple", "Cat", "Ball",
"Cartoon", "Banana", "Avocado" };

// sorting the String array


Arrays.sort(arr);

for (String i : arr) {


System.out.print(i + " ");
}
}
}

Output
Apple Avocado Ball Banana Cartoon Cat

13. // Java program deals with all operation of a dynamic array


// add, remove, resize memory of array is the main feature
public class DynamicArray {

// create three variable array[] is a array,


// count will deal with no of element add by you and
// size will with size of array[]
private int array[];
private int count;
private int size;
// constructor initialize value to variable

public DynamicArray()
{
array = new int[1];
count = 0;
size = 1;
}
// function add an element at the end of array

public void add(int data)


{

// check no of element is equal to size of array


if (count == size) {
growSize(); // make array size double
} // insert element at end of array
array[count] = data;
count++;
}

// function makes size double of array


public void growSize()
{

int temp[] = null;


if (count == size) {
// temp is a double size array of array
// and store array elements
temp = new int[size * 2];
{
for (int i = 0; i < size; i++) {
// copy all array value into temp
temp[i] = array[i];
}
}
}

// double size array temp initialize


// into variable array again
array = temp;

// and make size is double also of array


size = size * 2;
}

// function shrink size of array


// which block unnecessary remove them
public void shrinkSize()
{
int temp[] = null;
if (count > 0) {

// temp is a count size array


// and store array elements
temp = new int[count];
for (int i = 0; i < count; i++) {

// copy all array value into temp


temp[i] = array[i];
}

size = count;

// count size array temp initialize


// into variable array again
array = temp;
}
}
// function add an element at given index

public void addAt(int index, int data)


{
// if size is not enough make size double
if (count == size) {
growSize();
}

for (int i = count - 1; i >= index; i--) {

// shift all element right


// from given index
array[i + 1] = array[i];
}
// insert data at given index
array[index] = data;
count++;
}

// function remove last element or put


// zero at last index
public void remove()
{
if (count > 0) {
array[count - 1] = 0;
count--;
}
}

// function shift all element of right


// side from given index in left
public void removeAt(int index)
{
if (count > 0) {
for (int i = index; i < count - 1; i++) {

// shift all element of right


// side from given index in left
array[i] = array[i + 1];
}
array[count - 1] = 0;
count--;
}
}

public static void main(String[] args)


{
DynamicArray da = new DynamicArray();

// add 9 elements in array


da.add(1);
da.add(2);
da.add(3);
da.add(4);
da.add(5);
da.add(6);
da.add(7);
da.add(8);
da.add(9);

// print all array elements after add 9 elements


System.out.println("Elements of array:");
for (int i = 0; i < da.size; i++) {
System.out.print(da.array[i] + " ");
}

System.out.println();

// print size of array and no of element


System.out.println("Size of array: " + da.size);
System.out.println("No of elements in array: "
+ da.count);
// shrinkSize of array
da.shrinkSize();

// print all array elements


System.out.println("Elements of array "
+ "after shrinkSize of array:");
for (int i = 0; i < da.size; i++) {
System.out.print(da.array[i] + " ");
}
System.out.println();

// print size of array and no of element


System.out.println("Size of array: " + da.size);
System.out.println("No of elements in array: "
+ da.count);

// add an element at index 1


da.addAt(1, 22);

// print Elements of array after adding an


// element at index 1
System.out.println("Elements of array after"
+ " add an element at index 1:");
for (int i = 0; i < da.size; i++) {
System.out.print(da.array[i] + " ");
}

System.out.println();

// print size of array and no of element


System.out.println("Size of array: " + da.size);
System.out.println("No of elements in array: "
+ da.count);

// delete last element


da.remove();

// print Elements of array after delete last


// element
System.out.println("Elements of array after"
+ " delete last element:");
for (int i = 0; i < da.size; i++) {
System.out.print(da.array[i] + " ");
}

System.out.println();

// print size of array and no of element


System.out.println("Size of array: " + da.size);
System.out.println("No of elements in array: "
+ da.count);

// delete element at index 1


da.removeAt(1);

// print Elements of array after delete


// an element index 1
System.out.println("Elements of array after"
+ " delete element at index 1:");
for (int i = 0; i < da.size; i++) {
System.out.print(da.array[i] + " ");
}
System.out.println();

// print size of array and no of element


System.out.println("Size of array: " + da.size);
System.out.println("No of elements in array: "
+ da.count);
}
}

Output
Elements of array : 1 2 3 4 5 6 7 8 9 10 11
No of elements in array : 11, Capacity of array :16

Capacity of array after shrinking : 11

After inserting at index 3


Elements of array : 1 2 3 50 4 5 6 7 8 9 10 11
No of elements in array : 12, Capacity of array :22

After delete last element Elements of array : 1 2 3 50 4 5 6 7 8 9 10


No of elements in array : 11, Capacity of array :11

After deleting at index 3 Elements of array : 1 2 3 4 5 6 7 8 9 10


No of elements in array : 10, Capacity of array :11

Searching 5 in array Element found at index : 4

14. Certainly! Here's a Java program that checks the number of occurrences of each
digit in a series of strings using arrays:

public class DigitOccurrencesCounter {


public static void main(String[] args) {
String[] inputStrings = {"abc123", "456xyz", "7890"};

int[] digitOccurrences = countDigitOccurrences(inputStrings);

for (int i = 0; i < digitOccurrences.length; i++) {


System.out.println("Digit " + i + ": " + digitOccurrences[i] + "
occurrences");
}
}

public static int[] countDigitOccurrences(String[] strings) {


int[] digitOccurrences = new int[10]; // One element for each digit (0 to
9)

for (String string : strings) {


for (char c : string.toCharArray()) {
if (Character.isDigit(c)) {
int digit = Character.getNumericValue(c);
digitOccurrences[digit]++;
}
}
}

return digitOccurrences;
}
}

15. WordCounter

public class WordCounter {


private String[] words;
private int[] counts;

public WordCounter(String[] words) {


this.words = words;
counts = new int[words.length];
for (int i = 0; i < words.length; i++) {
counts[i] = 0;
}
}

public int getCount(String word) {


for (int i = 0; i < words.length; i++) {
//String w = words[i];
if (words[i].equals(word)) {
return counts[i];
}
}

return -1;

public void log(String word) {


for (int i = 0; i < words.length; i++) {
//String w = words[i];
if (words[i].equals(word)) {
counts[i] = counts[i] + 1;
}
}
}

You might also like