0% found this document useful (0 votes)
12 views7 pages

Java Programs for String and Array Manipulations

Uploaded by

Chhanda Paul
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views7 pages

Java Programs for String and Array Manipulations

Uploaded by

Chhanda Paul
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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 [Link].*;


public class Gfg{
static String LongestCommonPrefix(String strs ['N'] ){
if (strs == null && [Link] == 0)
return "-1";
[Link](strs); // Sort the array of strings
// Get the first and last string after sorting
String first = strs[0];
String last = strs[[Link]()-1];
int minLength = [Link]([Link](),[Link]());
int i = 0;
//Find the common prefix between first and last string
while(i < [Link] && [Link](i) == [Link](i)){
i++;
}
if(i == 0){
return "-1"; // Check if there's no common prefix
return [Link](0,i); /// Return the common prefix
}
}
public static void main(){
Scanner sc = new Scanner([Link]);
[Link]("Enter the no. of elements");
int N = [Link]();
for (int i = 0; i < N; i++){
strs[i]= [Link]();
}
[Link]("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 [Link];

public class StringSearch {


public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);

// Input long string (size 15)


[Link]("Enter a long string (15 characters):");
String longString = [Link]();
while ([Link]() != 15) {
[Link]("Invalid length. Please enter a 15-character string:");
longString = [Link]();
}

// Input short string (between 2 and 4 characters)


[Link]("Enter a short string (2-4 characters):");
String shortString = [Link]();
while ([Link]() < 2 || [Link]() > 4) {
[Link]("Invalid length. Please enter a string between 2-4
characters:");
shortString = [Link]();
}

// Initialize variables
int count = 0;
String positions = "";

// Search for short string in long string


for (int i = 0; i <= [Link]() - [Link](); i++) {
if ([Link](i, i + [Link]()).equals(shortString)) {
count++;
positions += i + ", ";
}
} // Print results
[Link]("Short string '" + shortString + "' appears " + count + " times
in the long string.");
[Link]("Positions: " + [Link](0, [Link]() - 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([Link]);

// Prompt user for input


[Link]("Enter a string: ");
String str = [Link]();

// Convert string to character array


char[] charArray = [Link]();

// Swap odd position characters with even position characters


for (int i = 0; i < [Link] - 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
[Link]("Original String: " + str);
[Link]("Swapped String: " + swappedStr);

// c lose Scanner
[Link]();
}
}
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 [Link];

public class ArrayOperations {

public static void main(String[] args) {


Scanner scanner = new Scanner([Link]);

// Input number of elements


[Link]("Enter the number of elements: ");
int n = [Link]();

// Input array elements


int[] array = new int[n];
[Link]("Enter " + n + " elements:");
for (int i = 0; i < n; i++) {
array[i] = [Link]();
}

// Print original array


[Link]("Original Array: " + [Link](array));

// Input number to delete


[Link]("Enter a number to delete: ");
int numToDelete = [Link]();

// Delete the number from array


array = deleteElement(array, numToDelete);

// Print updated array


[Link]("Updated Array: " + [Link](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 [Link];


import [Link];

public class ArrayInsertion {

public static void main(String[] args) {


Scanner scanner = new Scanner([Link]);

// Input number of elements


[Link]("Enter the number of elements: ");
int n = [Link]();

// Input array elements


int[] originalArray = new int[n];
[Link]("Enter " + n + " elements:");
for (int i = 0; i < n; i++) {
originalArray[i] = [Link]();
}

// Print original array


[Link]("Original Array: " + [Link](originalArray));

// Create a new array with one extra cell


int[] newArray = new int[n + 1];

// Input number to insert and its position


[Link]("Enter a number to insert: ");
int numToInsert = [Link]();
[Link]("Enter the position (1-" + (n + 1) + ") to insert: ");
int position = [Link]();

// Validate position
if (position < 1 || position > n + 1) {
[Link]("Invalid position.");
return;
}

// Insert the number at the specified position


newArray = insertElement(originalArray, numToInsert, position - 1);
// Print updated array
[Link]("Updated Array: " + [Link](newArray));
}
public static int[] insertElement(int[] originalArray, int element, int position) {
int[] newArray = new int[[Link] + 1];

// Copy elements before the insertion point


[Link](originalArray, 0, newArray, 0, position);

// Insert the new element


newArray[position] = element;

// Copy elements after the insertion point


[Link](originalArray, position, newArray, position + 1,
[Link] - 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 [Link].*;


Public class SquareMatrix{
Public static void main() {
Scanner sc = new Scanner([Link]);
[Link](“Enter N ”);
Int N = [Link]();
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++){
[Link](arr[i][j]+ “”);}
[Link](); }
}}

You might also like