Java > String-1 > startWord (CodingBat Solution)

Problem:

Given a string and a second "word" string, we'll say that the word matches the string if it appears at the front of the string, except its first char does not need to match exactly. On a match, return the front of the string, or otherwise return the empty string. So, so with the string "hippo" the word "hi" returns "hi" and "xip" returns "hip". The word will be at least length 1.

startWord("hippo", "hi") → "hi"
startWord("hippo", "xip") → "hip"
startWord("hippo", "i") → "h"


Solution:

public String startWord(String str, String word) {
  int lenStr = str.length();
  int lenWord = word.length();
  if (lenStr == 0)
    return "";
  if (lenWord > lenStr)
    return "";
    
  if (word.length() == 1)
    return str.substring(0,1);
  else if (str.substring(1,lenWord).equals(word.substring(1,lenWord)))
    return str.substring(0,lenWord);
  else
    return "";
}


8 comments :

  1. what if (lenStr) is smaller than (lenWord) wouldnt it give a error [index out of bound...]

    ReplyDelete
  2. public String startWord(String str, String word) {
    String match = "";
    if ( !str.isEmpty()
    && str.length() >= word.length()
    && str.substring(1, word.length()).equals
    (word.substring(1,word.length()))) {
    match = str.substring(0,word.length());
    }
    return match;
    }

    ReplyDelete
  3. public String startWord(String str, String word) {

    if(str.length()>=word.length()
    && str.substring(1,word.length()).equals(word.substring(1,word.length())))
    return str.substring(0,word.length());

    else return "";
    }

    ReplyDelete
  4. public String startWord(String str, String word) {
    int wlen = word.length();
    int slen = str.length();
    if ((slen<1)||(wlen>slen))
    return "";
    if (str.substring(1,wlen).equals(word.substring(1,wlen)))
    return str.substring(0,1)+word.substring(1);
    else return "";
    }

    ReplyDelete
  5. public String startWord(String str, String word) {
    if(word.length()>str.length()){
    return "";
    }
    String result = String.valueOf(str.charAt(0));
    for(int i = 1; i < word.length(); i++) {
    if(str.length()!=0 && word.charAt(i) == str.charAt(i)){
    result = result+ word.charAt(i);
    } else {
    result = "";
    }
    }
    return result;
    }

    ReplyDelete
  6. public String startWord(String str, String word) {
    if(str.length() >= word.length()) {
    boolean startsWithWord = str.startsWith(word);
    if(startsWithWord)
    return word;
    if(str.charAt(0) != word.charAt(0) &&
    str.substring(1, word.length()).equals(word.substring(1))) {
    return str.substring(0, word.length());
    }
    }
    return "";

    }

    ReplyDelete
  7. if (str.indexOf(word) >= 0 && word.equals(str.substring(0, word.length())))
    return word;
    else if (str.length() >= 1 && str.indexOf(word.substring(1)) >= 0 && !(word.equals(str.substring(0,word.length()))))
    return str.substring(0, word.length());
    else
    return "";

    ReplyDelete
  8. if(str.length()>0) {
    if (str.startsWith(word) || str.substring(1).startsWith(word.substring(1))) {
    return str.substring(0, word.length());
    }
    }
    return "";

    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