0% found this document useful (0 votes)
16 views6 pages

Array and String Manipulation Programs

Uploaded by

V.Annapeachi
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)
16 views6 pages

Array and String Manipulation Programs

Uploaded by

V.Annapeachi
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

Programs on array and string

1)
public class MaxElemntInArray {

public static void main(String[] args) {

int[] a = {70,20,79,70,555};
[Link]("====================");
[Link]([Link]);
[Link]("========================");
int max = a[0];

for(int i=1 ; i<[Link] ; i++)


{
if(max < a[i])
{
max = a[i];
}
}

[Link](max);
}
}

Output:
====================
5
========================
555

2)
public class MinElementInArray {

public static void main(String[] args) {

int[] a = {70,20,79,70,555};
[Link]("====================");
[Link]([Link]);
[Link]("========================");
int min = a[0];
for(int i=1 ; i<[Link] ; i++)
{
if(min > a[i])
{
min = a[i];
}
}

[Link](min);
}
}

Output:
====================
5
========================
20

3)
public class ReplaceWithZero {

public static void main(String[] args) {

int[] a = {60,10,20,68,10,48};
int num=10;
for(int i=0 ; i<[Link] ; i++)
{
if(a[i]==num)
{
a[i]=0;
}
}

for(int i=0 ; i<[Link] ; i++)


{
[Link](a[i]+" ");
}
}
}

Output:
60 0 20 68 0 48
4)
public class ReverseString {

public static void main(String[] args) {

String name = "roja";

int length = [Link]();

String reverse ="";

for(int i=length-1 ; i>=0 ; i--)


{
reverse = reverse+[Link](i);
}

[Link](reverse);

[Link]("=================");

if([Link](name))
{
[Link]("palindrome");
}
else {
[Link]("not palindrome");
}
}
}

Output:
ajor
=================
not palindrome

5)
public class SearchElementInArray {

public static void main(String[] args) {


int[] a = {900,67,19,80,99};

int ele =90;


int flag = 0;
for(int i=0 ; i<[Link] ; i++)
{
if(a[i]==ele)
{
flag=1;
[Link]("element found");
break;
}
}

if(flag == 0)
{
[Link]("element not found");
}

}
}
Output:
element not found

6) public class BubbleSort {

public static void main(String[] args) {

int[] a = {50,10,100,80};

int temp;

for(int i=0 ; i<[Link]-1 ;i++)


{
int flag=0;
for(int j =0 ; j<[Link]-1 ;j++)
{
//if(a[j] < a[j+1])-->descending order
if(a[j] > a[j+1])//-->ascending order
{
temp = a[j];
a[j]=a[j+1];
a[j+1]=temp;

flag=1;
}
}
if(flag==0)
{
break;
}
}

for(int i=0 ; i<[Link] ; i++)


{
[Link](a[i]+" ");
}
}
}

Output:
10 50 80 100

7)
public class ToUpperCase {

public static void main(String[] args) {

String s1 = "ABCD";

char[] a1 = [Link]();

for(int i=0 ; i<[Link] ; i++)


{
// if(a1[i]>='a' && a1[i]<='z')
if(a1[i]>='A' && a1[i]<='Z')
{
// a1[i]= (char) (a1[i]-32);
a1[i]= (char) (a1[i]+32);
}
}

String str = new String(a1);

[Link](str);
}
}

Output:
abcd
8)
public class CountPrimeNumber {

public static void main(String[] args) {

int[] a = {2,3,6,7};
int noOfPrimeNum=0;

for(int i=0 ; i<[Link] ; i++)


{
int count=0;
for(int j=1 ; j<=a[i] ; j++)
{
int num = a[i];
if(num%j==0)
{
count++;
}
}

if(count==2)
{
noOfPrimeNum++;
}
}

[Link](noOfPrimeNum);
}
}

Output:
3

Common questions

Powered by AI

The approach in 'CountPrimeNumber' iteratively checks each element in the array to see if it's divisible by any number other than 1 and itself, counting it as a prime if there are exactly two divisors. While this method correctly identifies prime numbers, it's inefficient for large arrays, as it performs redundant checks. A more efficient way would be the Sieve of Eratosthenes, which reduces the number of checks by marking non-prime numbers in advance .

The logical flaw in the reverse string program is that it directly compares the reversed string to the original string without ignoring case sensitivity or spaces, which could incorrectly identify palindromes. To accurately check for palindromes, the program should convert all characters to a consistent case (e.g., lowercase) and remove non-alphanumeric characters before comparison .

The 'ToUpperCase' program mistakenly converts uppercase letters to lowercase by adding 32 to the ASCII value of characters which fall in the range 'A' to 'Z'. This operation effectively transforms uppercase to lowercase, a useful feature in text normalization tasks that require consistent case presentation, ensuring uniformity in datasets irrespective of their original cases. A conscious use of such transformations is crucial for tasks such as data preprocessing and standardization .

The 'BubbleSort' program repeatedly steps through the array, comparing adjacent elements and swapping them if they're in the wrong order. This process continues until no swaps are needed, ensuring the array is sorted. An optimization involves adding a flag to detect if any swaps occurred in an iteration; if none occur, the array is already sorted, and the loop can terminate early, thus potentially reducing the number of iterations compared to a traditional bubble sort .

The 'MinElementInArray' program demonstrates a simple selection algorithm, finding the smallest element in one pass by initializing the first element as the minimum and updating it upon finding a lesser element. This approach is optimal for scenarios requiring only a single minimum value retrieval from a dataset, where direct comparison suffices and performance requirements align with the O(n) complexity intrinsic to its one-pass design .

The 'ReplaceWithZero' program directly replaces specified numbers with zero, which can lead to data integrity issues if zeros hold meaning within the data context (e.g., legitimate zero values or as a placeholder). This can skew data analyses or lead to incorrect assumptions. To mitigate issues, it would be prudent to use a symbol or number not already present in the dataset for marking, or maintain a separate array to track positions, ensuring the original data remains unaltered .

The 'ReplaceWithZero' program scans through the array and replaces any occurrences of a specified number with zero. This operation alters the original data, which is effective for data cleaning or marking certain elements as inactive without altering array structure. However, this approach might introduce zero-padding issues if zeros hold significance in the dataset, necessitating careful handling .

The 'SearchElementInArray' program employs a linear search, scanning each element until it finds the target or exhausts the array. This approach has a time complexity of O(n), making it inefficient for large datasets due to its linear growth in search time relative to data size. Improvements include using binary search on sorted arrays to achieve O(log n) complexity or employing hash-based methods for constant time O(1) average complexity search performance depending on implementation specifics .

The Java program finds the maximum element in an array by initializing the first element as the maximum and then iterating through the array. During each iteration, it compares the current element with the maximum. If the current element is greater, it updates the maximum. This method is efficient as it requires only a single linear pass through the array, making its time complexity O(n), where n is the number of elements .

The 'BubbleSort' algorithm has a time complexity of O(n^2) due to its nested loop structure, where n represents the number of elements in the array. Its quadratic growth significantly limits utility for large datasets as the number of comparisons increases rapidly. For extensive sorting tasks, more efficient algorithms like QuickSort or MergeSort with average complexities of O(n log n) are preferred, offering better scalability and processing speed .

You might also like