LPU Java Unit 2 Lecture 14 RB | LPUCOLAB

LPU Java Unit 2 Lecture 14 RB | LPUCOLAB

LPU_Java_Unit 2_Lecture 14_RB

1. Problem Statement

Meet Jancy, a diligent student learning to master programming. She is working on a project that requires her to process text data. Today, she needs to convert a given string to lowercase to ensure consistent and uniform text.

Input format: - The input consists of a single line of string.

Output format: - The output prints the input string converted to all lowercase letters.

Code constraints: - The input string will consist of at most 1000 characters. - The string may contain letters (uppercase and lowercase), digits, and special symbols.

Solution:

import java.util.Scanner;

public class LowercaseConverter {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String inputString = scanner.nextLine();

        // Convert the input string to lowercase
        String lowercaseString = inputString.toLowerCase();

        // Print the lowercase string
        System.out.println(lowercaseString);

        scanner.close();
    }
}

2. Problem Statement

Arjun is developing a text search tool to locate specific phrases within a larger text. He wants to write a program that takes a main string and a target substring as input. The program should check if the target substring exists within the main string and display it if found. If the substring is not present, print an appropriate message indicating its absence.

Input format: - The first line of input contains a string representing the main string. - The second line of input contains a string representing the target substring.

Output format: - The output prints the found substring or "Substring not found" if the target substring is not present in the main string.

Solution:

import java.util.Scanner;

public class SubstringSearch {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // Read main string and target substring
        String mainString = scanner.nextLine();
        String targetSubstring = scanner.nextLine();

        // Check if substring exists
        int index = mainString.indexOf(targetSubstring);

        // Print result
        if (index != -1) {
            System.out.println("Found Substring: " + targetSubstring);
        } else {
            System.out.println("Substring not found");
        }

        scanner.close();
    }
}