Problem:
A sandwich is two pieces of bread with something in between. Return the string that is between the first and last appearance of "bread" in the given string, or return the empty string "" if there are not two pieces of bread.
getSandwich("breadjambread") → "jam"
getSandwich("xxbreadjambreadyy") → "jam"
getSandwich("xxbreadyy") → ""
Solution:
public String getSandwich(String str) { int len = str.length(); String tmpString = ""; String finalString = ""; int start = 0; int finish = 0; boolean found = false; if (len <= 10) return ""; for (int i = 0; i < len - 4; i++) { tmpString = str.substring(i, i+5); if (tmpString.equals("bread") && found == true) finish = i; if (tmpString.equals("bread") && found == false) { start = i+5; found = true; } } finalString = str.substring(start,finish); return finalString; }
Alternatively,
ReplyDeletepublic String getSandwich(String str) {
int find=0;
int bind=0;
if (str.length()<=10)
return "";
for (int i = 0; i<str.length()-5;i++)
{
if (find==0 && str.substring(i,i+5).equals("bread"))
find=i+5;
if (bind==0 && str.substring(str.length()-5-i,str.length()-i).equals("bread"))
bind=str.length()-5-i;
}
return str.substring(find,bind);
}
Too complicated both. I don't get why.
ReplyDeleteCheck my code:
public String getSandwich(String str) {
int first = str.indexOf("bread");
int last = str.lastIndexOf("bread");
if (first == last) return "";
return str.substring(first + 5, last);
}
Awesome
DeleteCool
DeleteAwesome, That's magic!
Deletepublic String getSandwich(String str) {
ReplyDeleteint fb = str.indexOf("bread");
int lb = str.lastIndexOf("bread");
String sw = new String();
if (fb >= 0 && lb > 0 && fb != lb){
sw = str.substring(fb + 5,lb);
}
return sw;
}
what about this
ReplyDeletepublic String getSandwich(String str) {
int firstPos = str.indexOf("bread");
int lastPos = str.lastIndexOf("bread");
if ( firstPos != -1 && lastPos != -1 && firstPos != lastPos )
return str.substring(firstPos+5, lastPos);
else
return "";
}
public String getSandwich(String str)
ReplyDelete{
String result ="";
int br = str.indexOf("bread")+5;
int lbr =str.lastIndexOf("bread");
for(int i =br; i<lbr;i++)
result+=str.substring(i,i+1);
return result;
}
// how I did it
ReplyDeletepublic String getSandwich(String str) {
String result = "";
String bread = "bread";
int len = str.length();
if (len > 10) {
int first = str.indexOf(bread);
int last = str.lastIndexOf(bread);
result = str.substring(first + 5, last);
}
return result;
}
public String getSandwich(String str) {
ReplyDeleteif (str.length() > 10)
return str.substring(str.indexOf("bread")+5,str.lastIndexOf("bread"));
return "";
}
uhh :)
Deletepublic String getSandwich(String str) {
ReplyDeleteString result = "";
int start = 0,
stop = 0;
for (int i = 0; i < str.length()-5; i++ )
{
if (str.substring(i, i+5).equals("bread") )
{
start = i+5;
break;
}
}
for (int i = str.length()-5; i >= 0; i-- )
{
if (str.substring(i, i+5).equals("bread") )
{
stop = i;
if (stop > start)
result = str.substring(start, stop);
break;
}
}
return result;
}
return str.indexOf("bread") != str.lastIndexOf("bread") ? str.substring(str.indexOf("bread") + 5, str.lastIndexOf("bread")): "";
ReplyDeletepublic String getSandwich(String str) {
ReplyDeleteif(str.length()<10) return "";
String b = "bread";
for(int i=0; i0; i--){
if(str.substring(i-5, i).equals(b)){
str = str.substring(0,i-5); break;}}
return str;
}
public String getSandwich(String str) {
Deleteif(str.length()<10) return "";
String b = "bread";
for(int i=0; i0; i--){
if(str.substring(i-5, i).equals(b)){
str = str.substring(0,i-5); break;}}
return str;
}
public String getSandwich(String str) {
ReplyDeleteif(str.length()<10) return "";
String b = "bread";
for(int i=0; i0; i--){
if(str.substring(i-5, i).equals(b)){
str = str.substring(0,i-5); break;}}
return str;
}
I am only beginners in programming, but as I see u have a few elementary mistakes here. So if you dont want to do such miskates again, I advise u to try learn something from this site that made just for everyone who are interested in Java, https://explainjava.com/for-each-loop-java/ for each loop java here.
ReplyDeleteint count =0;
ReplyDeleteint a =99;
int b =99;
String result ="";
for (int i=0; i<=str.length()-5; i++)
{
if (str.substring(i,i+5).equals("bread") && count!=1)
{
count = 1;
b=i;
}
if (count==1)
{
for (a=b+5; a<=str.length()-5; a++)
{
if (str.substring(a,a+5).equals("bread"))
result = str.substring(b+5,a);
}
}
}
return result;
public String getSandwich(String str) {
ReplyDeleteString bread = "bread";
int startBread = str.indexOf(bread);
int lastBread = str.lastIndexOf(bread);
if(str.length() < 10)
{
return "";
}
String substr = str.substring(startBread+5,lastBread);
return substr;
}
public String getSandwich(String str) {
ReplyDeleteif(str.length()<10) return "";
if(str.substring(0,5).equals("bread")) {
if(str.substring(str.length()-5).equals("bread")) return str.substring(5,str.length()-5);
return getSandwich(str.substring(0,str.length()-1));
}return getSandwich(str.substring(1));
}
public class Stringg {
ReplyDeletepublic static void main(String args[]) {
Scanner s=new Scanner(System.in);
System.out.println("enter a string");
String sb=new String(s.nextLine());
if(sb.contains("bread")) {
int m= sb.indexOf("bread");
System.out.println(sb.substring(0, m)+sb.substring(m+5));
}
else {
System.out.println(sb);
}
}}
// could work for some cases.
Nice article. Thanks for sharing.
ReplyDeleteCheers,
http://www.flowerbrackets.com/iterate-over-array-using-foreach-loop-in-java/
public String getSandwich(String str) {
ReplyDeleteString temp="";
int first=str.indexOf("bread");
int last = str.lastIndexOf("bread");
if (last<0 || first<0 ||(first==last))
return temp;
if(last>0&& first>=0)
temp=str.substring(first+5, last);
return temp;
}
int count =0;
ReplyDeleteint n= str.indexOf('d');
String result =str.substring(n+1);
for (int i=0 ; i=2)
return result.substring(0,result.lastIndexOf("bread"));
return "";
public String getSandwich(String str) {
ReplyDeleteint fIdx = str.indexOf("bread") ;
int lIdx = str.lastIndexOf("bread") ;
String result = "";
if (str.length() < 10){
return result;
}
result += str.substring(fIdx + 5, lIdx);
return result;
}
public String getSandwich(String str) {
ReplyDeleteString s="";
int first=str.indexOf("bread");
int last=str.lastIndexOf("bread");
int len= str.length();
String sample="";
if(first!=last)
sample= str.substring(first+4+1,last);
return sample;
}
public String getSandwich(String str) {
ReplyDeleteint last = str.lastIndexOf("bread");
int first = str.indexOf("bread");
if(!str.contains("bread") || (last == first)){
return "";
} else{
return str.substring(first+5, last);
}
}
String emp="";
ReplyDeleteint last=str.lastIndexOf("bread");
int first=str.indexOf("bread");
if(str.contains("bread")&&str.length()>10)
return str.substring(first+5,last);
return emp;
public String getSandwich(String str) {
ReplyDeleteif (str.indexOf("bread") != str.lastIndexOf("bread"))
return str.substring(str.indexOf("bread")+5, str.lastIndexOf("bread"));
return "";
}
Nice!
Deletepublic String getSandwich(String str) {
ReplyDeleteint j = str.length() - 5;
for (int i = 0; i < str.length(); i++)
{
if (j >= 0 && !str.substring(j, j + 5).equals("bread"))
{
j--;
}
else if (j > i && i + 5 <= str.length() && str.substring(i, i + 5).equals("bread") && str.substring(j, j + 5).equals("bread"))
{
return str.substring(i + 5, j);
}
}
return "";
}
public String getSandwich(String str) {
ReplyDeleteif (str.length() <= 10){
return "";
}
return str.substring((str.indexOf("bread") + 5), str.lastIndexOf("bread"));
}
public String getSandwich(String str) {
ReplyDeletereturn (str.length() <= 10)? "":
str.substring((str.indexOf("bread") + 5), str.lastIndexOf("bread"));
}
Laura lassoon mirchi pyaaz!!!
ReplyDeletepublic String getSandwich(String str) {
int first = str.indexOf("bread");
int last = str.lastIndexOf("bread");
if(first == last) return "";
return str.substring(first + 5, last);
}
What is the point of the booolean?
ReplyDeletejust having fun find me at www.dantewhite.com
ReplyDeletepublic String getSandwich(String str) {
StringBuilder sb = new StringBuilder();
int len = str.length();
for (int i = 0; i < str.length()-4; i++) {
if (str.substring(i,i+5).equals("bread")) {
int j = str.length();
while(j != i+5) {
if ( str.substring(j-5,j).equals("bread")) {
return str.substring(i+5,j-5);
}
j--;
}
}
}
return "";
}
In Javascript
ReplyDeletefunction getSandwich(str){
let firstIndexOfBread = str.indexOf('bread');
let lastIndexOfBread = str.lastIndexOf('bread');
if (firstIndexOfBread == lastIndexOfBread) {
return '';
}
let strTwo = str.slice(firstIndexOfBread + 5, lastIndexOfBread);
return strTwo
}
public String getSandwich(String str) {
ReplyDeleteint count=0;
int len=str.length();
if(len >=10){
for(int i=0;i=2){
int sInd=str.indexOf("bread");
int lInd=str.lastIndexOf("bread");
String midStr=str.substring(sInd+5,lInd);
return midStr;
}
return new String("");
}
public String getSandwich(String str) {
ReplyDeleteString result = "";
String bread = "bread";
int start = str.indexOf(bread);
int end = str.lastIndexOf(bread);
if(start == -1 || end == -1 || start == end)
return result;
else
result += str.substring(start+5, end);
return result;
}
public String getSandwich(String str) {
ReplyDeleteString result = "";
if (str.contains("bread")) {
while (!str.startsWith("bread")) {
str = str.substring(1);
}
while (!str.endsWith("bread")) {
str = str.substring(0, str.length() - 1);
}
if (str.startsWith("bread") && str.endsWith("bread") && str.length() > 10)
result = str.substring(5, str.length() - 5);
}
return result;
}
public String getSandwich(String str) {
ReplyDeleteString tar = "bread";
String acc = "";
String sandwich = "";
int start = 0;
int end = 0;
int size = str.length();
for(int i = 0; i <= size - 5; i++){
acc = "";
for(int j = i; j < i + 5; j++){
acc = acc + str.charAt(j);
}
if(acc.equals(tar)){
start = i + 5;
break;
}
}
for(int i = start; i <= size - 5; i++){
acc = "";
for(int j = i; j < i + 5; j++){
acc = acc + str.charAt(j);
}
if(acc.equals(tar)){
end = i;
}
}
for(int i = start; i < end; i++){
sandwich = sandwich + str.charAt(i);
}
return sandwich;
}
public String getSandwich(String str)
ReplyDelete{
if(str.length() > 10)
{
int frontBread = str.indexOf("bread");
int backBread = str.lastIndexOf("bread");
return str.substring(frontBread+5,backBread);
}
return "";
}
sa
ReplyDeletepublic String getSandwich(String str) {
ReplyDeleteint breadIndex = str.lastIndexOf("bread");
if(str.length() < 10) return "";
for(int i = 0; i < str.length()-4; i++){
if(str.substring(i, i+5).equals("bread") && i < breadIndex){
return str.substring(i+5, breadIndex);
}
}
return null;
}
public String getSandwich(String str) {
ReplyDeletereturn str.length() > 10 ? str.substring(str.indexOf("bread")+5, str.lastIndexOf("bread")) : "";
}
public String getSandwich(String str) {
ReplyDeleteif(str.indexOf("bread", str.indexOf("bread") + 1) > -1){
int f = str.indexOf("bread");
int l = str.lastIndexOf("bread");
return str.substring(f+5,l);
} else {
return "";
}
}
public String getSandwich(String str) {
ReplyDeleteif(str.length()< 11){
return "";
}
StringBuilder builder = new StringBuilder();
builder.append(str.replaceFirst("bread"," "));
builder.delete(builder.lastIndexOf("bread"),builder.length());
builder.delete(0,builder.indexOf(" ")+1);
return builder.toString();
}