Day 6 - 20 August, 2024

Program 19: WAP to print your name on user input with string methods

import java.util.*; class P1 { public static void main(String[] args) { String name; Scanner sc = new Scanner(System.in); System.out.print("Enter you name : "); name = sc.nextLine(); System.out.println("Your name in uppercase : " + name.toUpperCase()); System.out.println("Your name in lowercase : " + name.toLowerCase()); System.out.println("Length of Your Name : " + name.length()); } } Copy Code
Expected Output:
Input:
Enter your name: Hardik Srivastava

Output:
Your name in uppercase : HARDIK SRIVASTAVA
Your name in lowercase : hardik srivastava
Length of Your Name : 17

Program 20: WAP to compare two strings for equality

import java.util.*; class P2 { public static void main(String[] args) { String str1, str2; Scanner sc = new Scanner(System.in); System.out.print("Enter your first string to compare : "); str1 = sc.nextLine(); System.out.print("Enter your second string to compare : "); str2 = sc.nextLine(); if (str1.equalsIgnoreCase(str2)) { System.out.print("Both strings are same."); } else { System.out.print("Both strings are not same."); } } } Copy Code
Expected Output:
Input:
Enter your first string to compare : Java
Enter your second string to compare : java

Output:
Both strings are same.

Program 21: WAP to create a word counter

import java.util.*; class P3 { public static void main(String[] args) { String str; Scanner sc = new Scanner(System.in); System.out.print("Enter your String to count the words : "); str = sc.nextLine(); String[] words = str.split(" "); System.out.print("Here are the words counts : " + words.length); } } Copy Code
Expected Output:
Input:
Enter your String to count the words : Java is a powerful programming language

Output:
Here are the words counts : 6

Program 22: WAP to replace a word in string using replace method

import java.util.*; class P4 { public static void main(String[] args) { String str, fw, rw; Scanner sc = new Scanner(System.in); System.out.print("Enter a Sentence : "); str = sc.nextLine(); System.out.print("Find What ? "); fw = sc.nextLine(); System.out.print("Replace What ? "); rw = sc.nextLine(); System.out.print("Here is the modified sentence : " + str.replace(fw, rw)); } } Copy Code
Expected Output:
Input:
Enter a Sentence : I love programming in Java
Find What ? Java
Replace What ? Python

Output:
Here is the modified sentence : I love programming in Python