Program Name:
SubstringDemo
Description:
The program allows users to extract specific portions of a string using Java's substring
method. Users can choose to extract:
Input:
- A full string (e.g., 'Hello, welcome to Java programming').
- Start and end indices (if extracting a middle portion).
Output:
- Extracted substring based on the chosen operation.
1. Start 2. Accept a string from the user. 3. Display a menu with options: - Extract the first word. - Extract a middle portion. - Extract the last word. 4. Based on the user's choice: - Use `substring` to extract the desired portion: - For the first word: Find the first space index and extract from 0 to that index. - For the middle portion: Prompt the user for start and end indices. - For the last word: Find the last space index and extract from there to the end. 5. Display the extracted substring. 6. End
Input:
Enter a string:
Hello, welcome to Java programming
Choose an option:
1. Extract the first word
2. Extract a middle portion
3. Extract the last word
Output (for each choice):
- Option 1: First word: Hello,
- Option 2: (User inputs start = 7, end = 14) Middle portion: welcome
- Option 3: Last word: programming