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
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
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
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