1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Making more efficient for loop

  1. Jan 14, 2017 #1
    1. The problem statement, all variables and given/known data
    Given a string and a non-empty word string, return a string made of each char just before and just after every appearance of the word in the string. Ignore cases where there is no char before or after the word, and a char may be included twice if it is between two words.

    2. Relevant equations


    3. The attempt at a solution
    Code (Java):

    public String wordEnds(String str, String word) {
      String newString = "";
     
      for(int i=0; i<str.length()-word.length(); i++) {
        int finalCheck = str.length()-word.length();
        if(str.substring(i, i+word.length()).equals(word)) {
          if(i!=0) {
            newString += "" + str.charAt(i-1);
          }
          newString += "" + str.charAt(i+word.length());
         
        }
      }
      return newString;
    }

     
    This code works in all cases unless the word is at the last index(e.g: wordEnds("abc1xyz1abc", "abc") → "11" got "1"
    wordEnds("abc1abc1abc", "abc") → "1111" got "111") It isn't adding the last char before the last index.)
    So I made this code which works but it's not incorporated for inside the loop which I'd like to do
    Code (Java):

    public String wordEnds(String str, String word) {
      String newString = "";
     
      for(int i=0; i<str.length()-word.length(); i++) {
        int finalCheck = str.length()-word.length();
        if(str.substring(i, i+word.length()).equals(word)) {
          if(i!=0) {
            newString += "" + str.charAt(i-1);
          }
          newString += "" + str.charAt(i+word.length());
         
        }
      }
      int finalOccurence = str.length()-word.length();
      if(str.length()>word.length()&&str.substring(finalOccurence).equals(word)) {
        newString+="" + str.charAt(finalOccurence-1);
      }
     
      return newString;
    }

     
     
  2. jcsd
  3. Jan 14, 2017 #2

    Mark44

    Staff: Mentor

    Without looking that closely at your code, it seems to me that you could put the logic of checking for the last word as an if statement inside the for loop. I would structure it so that the if clause executes if the word isn't the last word, and the else clause handles the case when the word is the last word. In other words, the part right after if is set up to execute most often.

    Also, as far as I can tell, since str and word are passed as parameters to your wordEnds() function, your code is using length() unnecessarily. I would calculate str.length() and word.length() once at the top of the function, and store these values in local variables. It's very inefficient to call a function multiple times in each loop iteration if the lengths aren't actually changing.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted