Good day.

There is a simple task: take a string and remove from it all the substrings of 3 characters. The substrings begin with 'y' and end with 'k', and from 'yakjkk', for example, you get 'jkk'.

The correct solution is:

public String stringCut(String str) { String result = ""; for (int i=0; i<str.length(); i++) { if (i+2<str.length() && str.charAt(i)=='y' && str.charAt(i+2)=='k') { i = i + 2; } else { result = result + str.charAt(i); } } return result; } 

My solution is very similar, it just comes from if not equal:

 public String stringСut(String str) { String result = ""; for (int i=0; i<str.length(); i++) { if (i+2<str.length() && str.charAt(i)!='y' && str.charAt(i+2)!='k') { result = result + str.charAt(i); } else { i = i + 2; } } return result; } 

This code is compiled, however, only an empty string or an erroneous result is returned. With what it can be connected?

Thank you.

Closed due to the fact that off-topic participants insolor , pavel , user207618, cheops , sercxjo 28 Aug '16 at 18:34 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • "The question is caused by a problem that is no longer reproduced or typed . Although similar questions may be relevant on this site, solving this question is unlikely to help future visitors. You can usually avoid similar questions by writing and researching a minimum program to reproduce the problem before publishing the question. " - insolor, pavel, community spirit, cheops, sercxjo
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • because the code is not identical - the condition is incorrectly inverted - Grundy
  • Understand Boolean algebra. In particular, with the rule of de Morgan - tutankhamun
  • But the condition was correctly inverted in the previous answer, right? - Dmitry08
  • Not. In your first example (a && b && c) . You did (a && not(b) && not(c)) , in response you tried to write (a && not(b) || not(c)) , and according to de Morgan's rule everything should be inverted (not(a) || not(b) || not(c)) . - tutankhamun
  • Sorry, everything worked perfectly, this time all the options. Thank you very much. - Dmitry08

1 answer 1

 i+2<str.length() 

This condition was not inverted correctly.

  • And && between terms. In general, in the comments to the question, everything has already been said. - insolor
  • Yes, along with && exactly like that. Thank you very much. - Dmitry08