Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
5 views2 pages

2 Lab Programs

The document is a Java program that demonstrates various string handling functions. It allows the user to input a string and provides functionalities such as calculating the string length, converting it to uppercase and lowercase, checking for a substring, and finding the index of a specific character. The program utilizes the Scanner class for user input and performs operations based on the input provided.
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)
5 views2 pages

2 Lab Programs

The document is a Java program that demonstrates various string handling functions. It allows the user to input a string and provides functionalities such as calculating the string length, converting it to uppercase and lowercase, checking for a substring, and finding the index of a specific character. The program utilizes the Scanner class for user input and performs operations based on the input provided.
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/ 2

1).

Java Program for string Handling Functions


import java.util.Scanner;

public class StringHandlingProgram {


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

// Get input from the user


System.out.print("Enter a string: ");
String input = scanner.nextLine();

// Display the length of the string


System.out.println("Length of the string: " + input.length());

// Convert the string to uppercase


String uppercase = input.toUpperCase();
System.out.println("Uppercase string: " + uppercase);

// Convert the string to lowercase


String lowercase = input.toLowerCase();
System.out.println("Lowercase string: " + lowercase);

// Check if the string contains a specific substring


System.out.print("Enter a substring to search for: ");
String substring = scanner.nextLine();
if (input.contains(substring)) {
System.out.println("String contains the substring.");
} else {
System.out.println("String does not contain the substring.");
}

// Get the index of a specific character in the string


System.out.print("Enter a character to search for: ");
char ch = scanner.nextLine().charAt(0);
int index = input.indexOf(ch);
if (index != -1) {
System.out.println("Character found at index " + index);
} else {
System.out.println("Character not found in the string.");
}

scanner.close();
}
}

You might also like