tag:blogger.com,1999:blog-5872623838295077986.post5386245350330822942..comments2024-03-20T01:07:56.465-07:00Comments on JavaProblems.com - Free coding problems and exercises: Java > String-1 > startWord (CodingBat Solution)Unknownnoreply@blogger.comBlogger14125tag:blogger.com,1999:blog-5872623838295077986.post-83453470696649666472022-04-02T15:06:49.401-07:002022-04-02T15:06:49.401-07:00Great Job!!Great Job!!nileshpawar1605https://www.blogger.com/profile/06499165150981168475noreply@blogger.comtag:blogger.com,1999:blog-5872623838295077986.post-77543625777175357492022-04-02T15:04:22.009-07:002022-04-02T15:04:22.009-07:00My Try:
public String startWord(String str, String...My Try:<br />public String startWord(String str, String word) {<br /> int len_word = word.length();<br /> if(len_word > str.length()) {<br /> return "";<br /> }<br /> if(str.substring(1, len_word).equals(word.substring(1))){<br /> return str.substring(0, len_word);<br /> }<br /> return "";<br />}nileshpawar1605https://www.blogger.com/profile/06499165150981168475noreply@blogger.comtag:blogger.com,1999:blog-5872623838295077986.post-52735277696572076672022-03-29T03:06:33.951-07:002022-03-29T03:06:33.951-07:00int wLen = word.length();
int sLen = str.length(...int wLen = word.length();<br /> int sLen = str.length();<br /> <br /> if (sLen >= wLen && str.substring(1, wLen).equals(word.substring(1, wLen))) <br /> return str.substring(0, wLen);<br /> return "";khushihttps://www.blogger.com/profile/11363137674976421526noreply@blogger.comtag:blogger.com,1999:blog-5872623838295077986.post-1189396510728110452022-03-07T01:27:07.019-08:002022-03-07T01:27:07.019-08:00public String startWord(String str, String word) {...public String startWord(String str, String word) {<br /> String ste = "";<br /> if (str.length() < word.length()) return "";<br /> if (word.length() > 1) {<br /> String tester = word.substring(1, word.length());<br /> if (str.contains(tester)) {<br /> ste = str.substring(0, word.length());<br /> }<br /> } else {<br /> ste = str.substring(0, word.length());<br /> }<br /> return ste;<br /> }Shohimardonhttps://www.blogger.com/profile/06885364183406230167noreply@blogger.comtag:blogger.com,1999:blog-5872623838295077986.post-53638005123498884662021-11-13T13:58:43.706-08:002021-11-13T13:58:43.706-08:00public String startWord(String str, String word) {...public String startWord(String str, String word) {<br />if(str.length()>0) {<br />if (str.startsWith(word) || str.substring(1).startsWith(word.substring(1))) {<br />return str.substring(0, word.length());}<br />}<br />return "";<br />}ewedaahttps://www.blogger.com/profile/03193416387787231303noreply@blogger.comtag:blogger.com,1999:blog-5872623838295077986.post-60578579906598319592020-12-19T14:06:34.717-08:002020-12-19T14:06:34.717-08:00public String startWord(String str, String word) {...public String startWord(String str, String word) {<br />if(word.length()>str.length()){<br /> return "";<br />}<br />else if(str.substring(0,word.length()).equals(word) || str.substring(1,word.length()).equals(word.substring(1,word.length()))){<br /> return str.substring(0,word.length());<br />}<br />else{<br /> return "";<br />}<br />}checknohttps://www.blogger.com/profile/11295007578228399372noreply@blogger.comtag:blogger.com,1999:blog-5872623838295077986.post-48099486207828570542020-08-13T22:51:21.936-07:002020-08-13T22:51:21.936-07:00if(str.length()>0) {
if (str.starts...if(str.length()>0) {<br /> if (str.startsWith(word) || str.substring(1).startsWith(word.substring(1))) {<br /> return str.substring(0, word.length());<br /> }<br /> }<br /> return "";AnarAghahttps://www.blogger.com/profile/14316583626612362652noreply@blogger.comtag:blogger.com,1999:blog-5872623838295077986.post-3386680166422896352020-05-02T06:32:06.008-07:002020-05-02T06:32:06.008-07:00if (str.indexOf(word) >= 0 && word.equa... if (str.indexOf(word) >= 0 && word.equals(str.substring(0, word.length())))<br /> return word;<br /> else if (str.length() >= 1 && str.indexOf(word.substring(1)) >= 0 && !(word.equals(str.substring(0,word.length()))))<br /> return str.substring(0, word.length());<br /> else<br /> return "";Anonymoushttps://www.blogger.com/profile/13304947057206593862noreply@blogger.comtag:blogger.com,1999:blog-5872623838295077986.post-73342694893618963932019-06-13T04:34:20.645-07:002019-06-13T04:34:20.645-07:00public String startWord(String str, String word) {...public String startWord(String str, String word) {<br /> if(str.length() >= word.length()) {<br /> boolean startsWithWord = str.startsWith(word);<br /> if(startsWithWord)<br /> return word;<br /> if(str.charAt(0) != word.charAt(0) && <br /> str.substring(1, word.length()).equals(word.substring(1))) {<br /> return str.substring(0, word.length());<br /> }<br /> }<br /> return "";<br /> <br />}<br />Knownhttps://www.blogger.com/profile/14284974007908292288noreply@blogger.comtag:blogger.com,1999:blog-5872623838295077986.post-81126745671116047942019-05-06T09:18:43.364-07:002019-05-06T09:18:43.364-07:00public String startWord(String str, String word) {...public String startWord(String str, String word) {<br /> if(word.length()>str.length()){<br /> return "";<br /> }<br /> String result = String.valueOf(str.charAt(0));<br /> for(int i = 1; i < word.length(); i++) {<br /> if(str.length()!=0 && word.charAt(i) == str.charAt(i)){<br /> result = result+ word.charAt(i);<br /> } else {<br /> result = "";<br /> }<br /> }<br /> return result;<br />}<br />Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-5872623838295077986.post-88995426770600270592019-01-12T08:29:03.437-08:002019-01-12T08:29:03.437-08:00public String startWord(String str, String word) {...public String startWord(String str, String word) {<br /> int wlen = word.length();<br /> int slen = str.length();<br /> if ((slen<1)||(wlen>slen))<br /> return "";<br /> if (str.substring(1,wlen).equals(word.substring(1,wlen)))<br /> return str.substring(0,1)+word.substring(1);<br /> else return "";<br />}Anonymoushttps://www.blogger.com/profile/04252849431776859135noreply@blogger.comtag:blogger.com,1999:blog-5872623838295077986.post-23911787128179215132018-04-06T13:42:02.827-07:002018-04-06T13:42:02.827-07:00public String startWord(String str, String word) {...public String startWord(String str, String word) {<br /> <br /> if(str.length()>=word.length() <br /> && str.substring(1,word.length()).equals(word.substring(1,word.length())))<br /> return str.substring(0,word.length());<br /> <br /> else return "";<br />}Anonymoushttps://www.blogger.com/profile/15314780490261343874noreply@blogger.comtag:blogger.com,1999:blog-5872623838295077986.post-24933188113243413432018-03-08T22:54:35.524-08:002018-03-08T22:54:35.524-08:00public String startWord(String str, String word) {...public String startWord(String str, String word) {<br /> String match = "";<br /> if ( !str.isEmpty() <br /> && str.length() >= word.length() <br /> && str.substring(1, word.length()).equals<br /> (word.substring(1,word.length()))) {<br /> match = str.substring(0,word.length());<br /> }<br /> return match;<br />}Navid Afzalnoreply@blogger.comtag:blogger.com,1999:blog-5872623838295077986.post-34522619101039603712017-03-17T07:59:54.245-07:002017-03-17T07:59:54.245-07:00what if (lenStr) is smaller than (lenWord) wouldnt...what if (lenStr) is smaller than (lenWord) wouldnt it give a error [index out of bound...]Anonymoushttps://www.blogger.com/profile/14596495374029847719noreply@blogger.com