Write a program that demonstrate program structure of java with use of String function implementation.
This program demonstrates the use of String functions in Java. It prompts the user to enter a string, and then uses the length() method to determine the length of the string, the toUpperCase() method to convert the string to uppercase, and the substring() method to extract a substring from the original string.
Note that the substring() method requires two parameters: the starting index and the ending index. The program prompts the user to enter these values and then uses them to extract the desired substring.
This program demonstrates the basic structure of a Java program, including the use of imports, class definitions, method definitions, and user input/output.
import java.util.Scanner; public class StringFunctionDemo { public static void main(String[] args) { // Create a Scanner object to read user input Scanner scanner = new Scanner(System.in); // Prompt the user to enter a string System.out.print("Enter a string: "); String str = scanner.nextLine(); // Use the length() method to determine the length of the string int length = str.length(); System.out.println("The length of the string is: " + length); // Use the toUpperCase() method to convert the string to uppercase String upperCaseStr = str.toUpperCase(); System.out.println("The string in uppercase is: " + upperCaseStr); // Use the substring() method to extract a substring from the original string System.out.print("Enter the starting index: "); int startIndex = scanner.nextInt(); System.out.print("Enter the ending index: "); int endIndex = scanner.nextInt(); String subString = str.substring(startIndex, endIndex); System.out.println("The substring is: " + subString); } }
Comments
Post a Comment