Java > String-2 > getSandwich (CodingBat Solution)

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;
}


45 comments :

  1. Alternatively,

    public 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);
    }

    ReplyDelete
  2. Too complicated both. I don't get why.
    Check 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);
    }

    ReplyDelete
  3. public String getSandwich(String str) {
    int 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;
    }

    ReplyDelete
  4. what about this
    public 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 "";
    }

    ReplyDelete
  5. public String getSandwich(String str)
    {
    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;
    }

    ReplyDelete
  6. // how I did it

    public 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;
    }

    ReplyDelete
  7. public String getSandwich(String str) {
    if (str.length() > 10)
    return str.substring(str.indexOf("bread")+5,str.lastIndexOf("bread"));
    return "";
    }

    ReplyDelete
  8. public String getSandwich(String str) {
    String 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;
    }

    ReplyDelete
  9. return str.indexOf("bread") != str.lastIndexOf("bread") ? str.substring(str.indexOf("bread") + 5, str.lastIndexOf("bread")): "";

    ReplyDelete
  10. public String getSandwich(String str) {
    if(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;
    }

    ReplyDelete
    Replies
    1. public String getSandwich(String str) {
      if(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;
      }

      Delete
  11. public String getSandwich(String str) {
    if(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;
    }

    ReplyDelete
  12. 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.

    ReplyDelete
  13. int count =0;
    int 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;

    ReplyDelete
  14. public String getSandwich(String str) {

    String 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;
    }

    ReplyDelete
  15. public String getSandwich(String str) {
    if(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));
    }

    ReplyDelete
  16. public class Stringg {
    public 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.

    ReplyDelete
  17. Nice article. Thanks for sharing.

    Cheers,
    http://www.flowerbrackets.com/iterate-over-array-using-foreach-loop-in-java/

    ReplyDelete
  18. public String getSandwich(String str) {
    String 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;

    }

    ReplyDelete
  19. int count =0;
    int n= str.indexOf('d');
    String result =str.substring(n+1);

    for (int i=0 ; i=2)
    return result.substring(0,result.lastIndexOf("bread"));
    return "";

    ReplyDelete
  20. public String getSandwich(String str) {
    int 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;
    }

    ReplyDelete
  21. public String getSandwich(String str) {
    String 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;

    }

    ReplyDelete
  22. public String getSandwich(String str) {
    int last = str.lastIndexOf("bread");
    int first = str.indexOf("bread");

    if(!str.contains("bread") || (last == first)){
    return "";
    } else{
    return str.substring(first+5, last);
    }
    }

    ReplyDelete
  23. String emp="";
    int last=str.lastIndexOf("bread");
    int first=str.indexOf("bread");
    if(str.contains("bread")&&str.length()>10)
    return str.substring(first+5,last);
    return emp;

    ReplyDelete
  24. public String getSandwich(String str) {
        if (str.indexOf("bread") != str.lastIndexOf("bread"))
            return str.substring(str.indexOf("bread")+5,                                        str.lastIndexOf("bread"));
        return "";
    }

    ReplyDelete
  25. public String getSandwich(String str) {
    int 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 "";
    }

    ReplyDelete
  26. public String getSandwich(String str) {
    if (str.length() <= 10){
    return "";
    }
    return str.substring((str.indexOf("bread") + 5), str.lastIndexOf("bread"));
    }

    ReplyDelete
  27. public String getSandwich(String str) {
    return (str.length() <= 10)? "":
    str.substring((str.indexOf("bread") + 5), str.lastIndexOf("bread"));
    }

    ReplyDelete
  28. Laura lassoon mirchi pyaaz!!!

    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);
    }

    ReplyDelete
  29. What is the point of the booolean?

    ReplyDelete
  30. just having fun find me at www.dantewhite.com
    public 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 "";

    }

    ReplyDelete
  31. In Javascript

    function 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
    }

    ReplyDelete
  32. public String getSandwich(String str) {
    int 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("");
    }

    ReplyDelete
  33. public String getSandwich(String str) {
    String 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;
    }

    ReplyDelete
  34. public String getSandwich(String str) {
    String 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;
    }

    ReplyDelete
  35. public String getSandwich(String str) {
    String 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;
    }

    ReplyDelete
  36. public String getSandwich(String str)
    {
    if(str.length() > 10)
    {
    int frontBread = str.indexOf("bread");
    int backBread = str.lastIndexOf("bread");

    return str.substring(frontBread+5,backBread);
    }

    return "";
    }

    ReplyDelete
  37. public String getSandwich(String str) {
    int 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;
    }

    ReplyDelete
  38. public String getSandwich(String str) {
    return str.length() > 10 ? str.substring(str.indexOf("bread")+5, str.lastIndexOf("bread")) : "";
    }

    ReplyDelete

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact