Java Programs


Partial Search for a string in an array

  • Function to check whether a string exists in an array of strings.The function should do a partial search for the string.
Program

import java.util.Scanner;

public class StringSearch {

            private static Scanner input;
            private static int limit,flag=0;
            public static void main(String[] args) {
                       
                        input=new Scanner(System.in);
                        System.out.println("Enter the limit of the array:");
                        limit=input.nextInt();
                        String[] str=new String[limit];
                        System.out.println("Enter the strings of the array:");
                        for(int i=0;i<limit;i++) {
                                    str[i]=input.next();
                        }
                        StringSearch.search(str);

            }
           
            public static void search(String[] str) {
                        String item;
                        System.out.println("Enter the element to be searched:");
                        item=input.next();
                        for(int i=0;i<limit;i++) {
                                    if(str[i].contains(item)) {
                                                flag=1;
                                                System.out.println("String was found at the position:"+(i+1));
                                    }
                        }
                        if(flag==0) {
                                    System.out.println("No such element");
                        }
            }

}

Output

Enter the limit of the array:
5
Enter the strings of the array:
Sehwag
Sachin
Kohli
Yuvraj
Dhoni
Enter the element to be searched:
Yuv
String was found at the position:4


Row-Wise Sorting of Array elements

  • Program to sort array elements (row wise).
Program

import java.util.Scanner;

public class RowWiseSort {

            private static Scanner input;
            private static int r,c;
            public static void main(String[] args) {
                        input=new Scanner(System.in);
                        System.out.println("Enter the no. of rows and columns of the array:");
                        r=input.nextInt();
                        c=input.nextInt();
                        int[][] arr=new int[r][c];
                        System.out.println("Enter the elements of the array:");
                        for(int j=0;j<r;j++) {
                                    for(int i=0;i<c;i++) {
                                                arr[j][i]=input.nextInt();
                                    }
                                   
                        }
                        RowWiseSort ob=new RowWiseSort();
                        ob.sort(arr);

            }
           
            public void sort(int[][] arr) {
                        int temp;
                        for(int j=0;j<r;j++) {
                                    for(int i=0;i<c;i++) {
                                                for(int k=0;k<c-1;k++) {
                                                            if(arr[j][k]>arr[j][k+1]) {
                                                                        temp=arr[j][k];
                                                                        arr[j][k]=arr[j][k+1];
                                                                        arr[j][k+1]=temp;
                                                            }
                                                }
                                    }
                        }
                        System.out.println("The row vise sorted array is:");
                        for(int j=0;j<r;j++) {
                                    for(int i=0;i<c;i++) {
                                                System.out.print(arr[j][i]+"\t");
                                    }
                                    System.out.println();
                        }
            }

}

Output

Enter the no. of rows and columns of the array:
3 3
Enter the elements of the array:
3 2 1
6 5 4
9 8 7
The row vise sorted array is:
1          2          3         
4          5          6         
7          8          9         


No comments:

Post a Comment