1.Write a program to check whether two strings are equal or not.
import java.util.Scanner;
public class StringEqualityChecker
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
// Input first string
System.out.print("Enter the first string: ");
String firstString = scanner.nextLine();
// Input second string
System.out.print("Enter the second string: ");
String secondString = scanner.nextLine();
// Check if the strings are equal
if (areStringsEqual(firstString, secondString))
{
System.out.println("The strings are equal.");
}
else
{
System.out.println("The strings are not equal.");
}
// Close the scanner to avoid resource leak
scanner.close();
}
// Method to check whether two strings are equal
private static boolean areStringsEqual(String str1, String str2)
{
// Use equals method for string comparison
return str1.equals(str2);
}}
2.Write a program to display reverse string.
import java.util.Scanner;
public class ReverseString {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input string
System.out.print("Enter a string: ");
String inputString = scanner.nextLine();
// Display the reverse of the string
String reversedString = reverseString(inputString);
System.out.println("Reversed String: " + reversedString);
// Close the scanner to avoid resource leak
scanner.close();
// Method to reverse a string
private static String reverseString(String input) {
// Convert the string to a char array
char[] charArray = input.toCharArray();
// Reverse the char array
int start = 0;
int end = charArray.length - 1;
while (start < end) {
// Swap characters at start and end indices
char temp = charArray[start];
charArray[start] = charArray[end];
charArray[end] = temp;
// Move indices towards the center
start++;
end--;
// Convert the char array back to a string
return new String(charArray);