Java: Return the substring that is between the first and last appearance of the substring 'toast' in the given string, or return the empty string if substirng 'toast' does not exists
Java String: Exercise-69 with Solution
Write a Java program to return the substring that is between the first and last appearance of the substring 'toast' in the given string, or return an empty string if the substring 'toast' does not exist.
Visual Presentation:
Sample Solution:
Java Code:
import java.util.*;
// Define a class named Main
public class Main {
// Method to extract the middle of the bread based on certain conditions
public String pickMiddleOfBread(String stng) {
int len = stng.length(); // Get the length of the input string
String tmpString = ""; // Initialize a temporary string variable
String resultString = ""; // Initialize a result string variable
int st = 0; // Initialize starting index
int en = 0; // Initialize ending index
boolean found = false; // Initialize a boolean variable indicating if "toast" is found
// If the length of the input string is less than or equal to 10, return an empty string
if (len <= 10)
return "";
// Iterate through the characters of the input string
for (int i = 0; i < len - 4; i++) {
tmpString = stng.substring(i, i + 5); // Extract a substring of length 5 starting at index 'i'
// Check if the substring is "toast" and 'found' flag is true, update the ending index
if (tmpString.equals("toast") && found == true)
en = i;
// Check if the substring is "toast" and 'found' flag is false, update the starting index and set 'found' flag to true
if (tmpString.equals("toast") && found == false) {
st = i + 5;
found = true;
}
}
// Extract the middle of the bread based on the starting and ending indices
resultString = stng.substring(st, en);
return resultString; // Return the result
}
// Main method to execute the program
public static void main(String[] args) {
Main m = new Main(); // Create an instance of the Main class
String str1 = "sweettoastbuttertoast"; // Input string
// Display the given string and the new string using pickMiddleOfBread method
System.out.println("The given strings is: " + str1);
System.out.println("The new string is: " + m.pickMiddleOfBread(str1));
}
}
Sample Output:
The given strings is: sweettoastbuttertoast The new string is: butter
Flowchart:
Java Code Editor:
Improve this sample solution and post your code through Disqus
Previous: Write a Java program to read a string and returns after removing a specified character and its immediate left and right characters.
Next: Write a Java program to check whether a string is pq-balanced or not.
A String is pq-balanced if for all the p's in the string atleast one 'q' must exists right of the p's.But 'q' before the 'p' makes the pq-balanced false.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/java-exercises/string/java-string-exercise-69.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics