java - Check if character `'a'` is separated from `'b'` by three places -
folks. i'm doing every problem on codebyte.com
(i suggest website wants practice coding skills :)) , got stuck @ problem: return true
if characters a
, b
separated ``3 places anywhere in string @ least once; else return false
. examples:
input = "after badly" output = "false" input = "laura sobs" output = "true"
my code giving me false
every time write string
when in cases should return true
. smb please take look?
public static void main(string[] args) { scanner kbd = new scanner(system.in); system.out.println("please enter string: "); string mystring = kbd.nextline(); char[] myarray = mystring.tochararray(); boolean result = false; for(int = 0; < myarray.length; i++) { if(myarray[i] == 'a' && myarray[i+4] == 'b') result = true; else result = false; } system.out.println(result); }
once find true, going want print true , exit loop. of continue going through string if find true value!
public static void main(string[] args) { scanner kbd = new scanner(system.in); system.out.println("please enter string: "); string mystring = kbd.nextline(); char[] myarray = mystring.tochararray(); boolean result = false; for(int = 0; < myarray.length; i++) { if(myarray[i] == 'a' && myarray[i+4] == 'b'){ result = true; break; } } system.out.println(result); }
also unclear if looking strings "adddb" or "addb" because former i+4 , latter i+3
Comments
Post a Comment