Q 1)Write a program to find the longest common prefix from an array
of Strings. If there is no common prefix, print an equivalent Message.
ANS) import java.util.*;
public class Gfg{
static String LongestCommonPrefix(String strs ['N'] ){
if (strs == null && strs.length == 0)
return "-1";
Arrays.sort(strs); // Sort the array of strings
// Get the first and last string after sorting
String first = strs[0];
String last = strs[strs.length()-1];
int minLength = Math.min(first.length(),last.length());
int i = 0;
//Find the common prefix between first and last string
while(i < min.Length && first.charAt(i) == last.charAt(i)){
i++;
}
if(i == 0){
return "-1"; // Check if there's no common prefix
return first.substring(0,i); /// Return the common prefix
}
}
public static void main(){
Scanner sc = new Scanner(System.in);
System.out.println("Enter the no. of elements");
int N = sc.nextInt();
for (int i = 0; i < N; i++){
strs[i]= sc.nextLine();
}
System.out.println("Longest Common
Prefix :"+LongestCommonPrefic(strs));
}
}
Q 2)Write a program to input a long string and a short string, the long
string should be of minimum 15 characters and the short sting should
be between 2 and 4. Display the no. of times the short string has
occurred in the long string and also print the first occurrence of short
sting in long string.
ANS) import java.util.Scanner;
public class StringSearch {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input long string (size 15)
System.out.println("Enter a long string (15 characters):");
String longString = scanner.nextLine();
while (longString.length() != 15) {
System.out.println("Invalid length. Please enter a 15-character string:");
longString = scanner.nextLine();
}
// Input short string (between 2 and 4 characters)
System.out.println("Enter a short string (2-4 characters):");
String shortString = scanner.nextLine();
while (shortString.length() < 2 || shortString.length() > 4) {
System.out.println("Invalid length. Please enter a string between 2-4
characters:");
shortString = scanner.nextLine();
}
// Initialize variables
int count = 0;
String positions = "";
// Search for short string in long string
for (int i = 0; i <= longString.length() - shortString.length(); i++) {
if (longString.substring(i, i + shortString.length()).equals(shortString)) {
count++;
positions += i + ", ";
}
} // Print results
System.out.println("Short string '" + shortString + "' appears " + count + " times
in the long string.");
System.out.println("Positions: " + positions.substring(0, positions.length() - 2));
}
}
Q 3) Input a word and exchange its odd position characters with even
position characters.
ANS) public class SwapOddEvenCharacters {
public static void main(String[] args) {
// Create Scanner object for user input
Scanner scanner = new Scanner(System.in);
// Prompt user for input
System.out.print("Enter a string: ");
String str = scanner.nextLine();
// Convert string to character array
char[] charArray = str.toCharArray();
// Swap odd position characters with even position characters
for (int i = 0; i < charArray.length - 1; i += 2) {
// Swap characters
char temp = charArray[i];
charArray[i] = charArray[i + 1];
charArray[i + 1] = temp;
}
// Convert character array back to string
String swappedStr = new String(charArray);
// Print results
System.out.println("Original String: " + str);
System.out.println("Swapped String: " + swappedStr);
// c lose Scanner
scanner.close();
}
}
Q 4) A single dimentional array has ‘n’ number of elements. Now enter
a number and check for the presence of the number in the array, if
the number is present, then delete the number, and print the rest of
the numbers.
ANS) import java.util.Scanner;
public class ArrayOperations {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input number of elements
System.out.print("Enter the number of elements: ");
int n = scanner.nextInt();
// Input array elements
int[] array = new int[n];
System.out.println("Enter " + n + " elements:");
for (int i = 0; i < n; i++) {
array[i] = scanner.nextInt();
}
// Print original array
System.out.println("Original Array: " + Arrays.toString(array));
// Input number to delete
System.out.print("Enter a number to delete: ");
int numToDelete = scanner.nextInt();
// Delete the number from array
array = deleteElement(array, numToDelete);
// Print updated array
System.out.println("Updated Array: " + Arrays.toString(array));
}
Q5) Store ‘N’ number of elements in a single dimensional array, create
another array with extra cell from previous. Enter the elements in
original array. Enter a number and position ( 0 - N-1). Insert the
number in the new array at its respective position. Shift the remaining
element and print the original and resultant array.
ANS) import java.util.Arrays;
import java.util.Scanner;
public class ArrayInsertion {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input number of elements
System.out.print("Enter the number of elements: ");
int n = scanner.nextInt();
// Input array elements
int[] originalArray = new int[n];
System.out.println("Enter " + n + " elements:");
for (int i = 0; i < n; i++) {
originalArray[i] = scanner.nextInt();
}
// Print original array
System.out.println("Original Array: " + Arrays.toString(originalArray));
// Create a new array with one extra cell
int[] newArray = new int[n + 1];
// Input number to insert and its position
System.out.print("Enter a number to insert: ");
int numToInsert = scanner.nextInt();
System.out.print("Enter the position (1-" + (n + 1) + ") to insert: ");
int position = scanner.nextInt();
// Validate position
if (position < 1 || position > n + 1) {
System.out.println("Invalid position.");
return;
}
// Insert the number at the specified position
newArray = insertElement(originalArray, numToInsert, position - 1);
// Print updated array
System.out.println("Updated Array: " + Arrays.toString(newArray));
}
public static int[] insertElement(int[] originalArray, int element, int position) {
int[] newArray = new int[originalArray.length + 1];
// Copy elements before the insertion point
System.arraycopy(originalArray, 0, newArray, 0, position);
// Insert the new element
newArray[position] = element;
// Copy elements after the insertion point
System.arraycopy(originalArray, position, newArray, position + 1,
originalArray.length - position);
return newArray;
}
}
Q6) Write a program to input the rows and columns of an array and fill
up the square matrix in the following format :- ‘?’,’#’,’@’.
ANS) import java.util.*;
Public class SquareMatrix{
Public static void main() {
Scanner sc = new Scanner(System.in);
System.out.println(“Enter N ”);
Int N = sc.nextInt();
char arr[][] = new char [N][N];
for(int i = 0; i<+N; i++){
for (int j =0; j<=N; j++){
if(i==0 || i==N-1|| j==0|| j==N-1)
arr[i][j]= ‘@’;
else if (I == 1|| i==N-2||j==1||j==N-2)
arr[i][j]= ‘#’;
else
arr[i][j] = ‘?’; } }
for(int I = 0; i<=N; i++){
for (int j =0 ; j<=N; j++){
System.out.print(arr[i][j]+ “”);}
System.out.println(); }
}}