0% found this document useful (0 votes)
6 views3 pages

Lab 5

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Lab 5

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

1.

Writing program to demonstrate immutable property in strings using your Name and
Surname

Code:
public class q1 {
public static void main(String[] args) {
String firstName = "Govinda";
String lastName = "Rana";
String fullName = firstName + " " + lastName;
System.out.println("Original Full Name: " + fullName);
fullName = fullName.replace("Govinda", "Samjhana");
System.out.println("Modified Full Name: " + fullName);
System.out.println("Original First Name: " + firstName);
System.out.println("Original Last Name: " + lastName);
}
}

Output:
Original Full Name: Govinda Rana
Modified Full Name: Samjhana Rana
Original First Name: Govinda
Original Last Name: Rana

2.Write a program to input two strings from the user. Based on it perform the following
string operations:

2.1. Convert the text to lower case of one of the string,


Code:
import java.util.Scanner;

public class q2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the first string: ");
String str1 = scanner.nextLine();
System.out.print("Enter the second string: ");
String str2 = scanner.nextLine();
String Str3 = str1.toLowerCase();
System.out.println("Original String 1: " + str1);
System.out.println("Lowercase String 1: " + Str3);
System.out.println("String 2: " + str2);
scanner.close();
}}

Output:
Enter the first string: GOVINDA
Enter the second string: RANA
Original String 1: GOVINDA
Lowercase String 1: govinda
String 2: RANA

2.2Search the 1 occurrence of any letter within the text for second string
st

import java.util.Scanner;

public class q3 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the first string: ");
String text = scanner.nextLine();

System.out.print("Enter the letters to search: ");


String searchLetters = scanner.nextLine();
int firstIndex = -1;
for (int i = 0; i < searchLetters.length(); i++) {
char ch = searchLetters.charAt(i);
int index = text.indexOf(ch);
if (index != -1 && (firstIndex == -1 || index < firstIndex)) {
firstIndex = index;
} }
if (firstIndex == -1) {
System.out.println("No matching letter found in the text.");
} else {
System.out.println("First occurrence of any letter from second
string in text: " + firstIndex);
}
scanner.close();
}}
Output:

Enter the first string : govinda


Enter the letters to search: d
First occurrence of any letter from second string in text: 5

2.3 Compare whether two strings are equal or not

import java.util.Scanner;

public class main {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the first string: ");
String str1 = scanner.nextLine();
System.out.print("Enter the second string: ");
String str2 = scanner.nextLine();
if (str1.equals(str2)) {
System.out.println("Using equals(): Strings are equal.");
} else {
System.out.println("Using equals(): Strings are not equal.");
}

scanner.close();
}
}

Output:
Enter the first string: Govinda
Enter the second string: govinda
Using equals(): Strings are not equal.

You might also like