Week_2.4

mail@pastecode.io avatar
unknown
plain_text
2 months ago
1.3 kB
2
Indexable
Never
// Importing required packages
import java.util.Scanner;
import java.util.Arrays;

// Driver Code
public class lab4{
    public static void main(String args[]){
        // Creating a Scanner class object to take user input
        Scanner scanner = new Scanner(System.in);

        // Part 1 
        System.out.print("Enter the number of names you wanna enter in the list: ");
        int size = scanner.nextInt();

        scanner.nextLine();

        String names[] = new String[size]; 
        for(int i=0;i<names.length;i++){
            System.out.print("Enter name at index "+i+" : ");
            names[i] = scanner.nextLine();
        }

        Arrays.sort(names);

        System.out.println("\nList of names after sorting :");
        for(int i=0;i<names.length;i++){
            System.out.printf("%d. %s\n",i+1,names[i]);
        }

        // Part 2
        System.out.print("Enter any string as input : ");
        String str = scanner.nextLine();

        System.out.print("Enter any word to look for : ");
        String word_search = scanner.nextLine();
        
        System.out.println("\nIs "+word_search+" present in the main string ?\nAns: "+str.contains(word_search));

        // Part 3
        System.out.println("\nAfter converting to uppercase : "+str.toUpperCase());
    }
}
Leave a Comment