Hello! A combination of two sentences is given. Need to swap them. Please write the algorithm.

Closed due to the fact that off-topic participants are user31688, VenZell , null , Pavel Mayorov , fori1ton 7 May '15 at 6:24 .

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

  • "Questions asking for help with debugging (" why does this code not work? ") Should include the desired behavior , a specific problem or error, and the minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, complete, repeatable example . " - Community spirit, VenZell, null, fori1ton
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • find the point (or?!) and by this symbol divide the line into two sentences. Then just make a concatenation. There is a true option that the first sentence may end in ..., but the same symbol may also be in the middle of a preposition. But 99% can be covered. - KoVadim
  • KoVadim tell me the algorithm for writing a program pliz - Abylay
  • Take a tutorial on java any there it is covered ... - ganelon
  • And phrases come only from words. I would be so ashamed to ask elementary things. - ganelon
  • Well, two sentences - Abylay


1 answer 1

The algorithm I wrote - if these are two sentences, then there must be corresponding signs that separate them. This is in most cases a point, a question mark and an exclamation mark (we do not consider exoticism). Therefore, we find such a symbol and break the sentence by it. Then do the exchange.

import java.util.*; import java.lang.*; class Main { private static String rotate(String s) { s = s.trim(); // удалим лишние пробелы в конце и начале int i = s.indexOf("."); / ищем точку // если точку не нашли либо точка - конец предложения, то пытаемся найти ! и ? if (i == -1 || i == s.length() - 1) i = s.indexOf("!"); if (i == -1 || i == s.length() - 1) i = s.indexOf("?"); // что то нашли, делаем разбивку if (i != -1 && i != s.length() - 1) { String s1 = s.substring(i+1); // первое предложение String s2 = s.substring(0, i); // второе return s1.trim() + " " + s2.trim(); // складываем их через пробел, предварительно вырезав лишнее } else return "Ничего не найдено!"; } public static void main (String[] args) throws java.lang.Exception { // А это три тестовых случая System.out.println(rotate("One text. Second text.")); System.out.println(rotate("One text? Second text.")); System.out.println(rotate("One text! Second text.")); } } 

Of course, the algorithm is simple and will not cope with the case of "One text ??? Second Text !!!!" correct, but this is a separate task.

  • thank you very much bro! Respect and respect you! - Abylay
  • four
    @KoVadim shame on you - DreamChild pm
  • What should I be ashamed of? - KoVadim
  • 2
    I would not spend my time on it, but I will tell you a story. Somehow a friend came to take something in there, for the first time the teacher sees, there is a girl, a graduate student, maybe she looked at him and put the highest score, he was shocked, like “for what?”, Her explanation is “the more I I will release the fools into the labor market, the more I will be valued as a specialist, "so ... make your own conclusions. - Gorets
  • one
    @Gorets: Talents need help, mediocrity will break through themselves. (c) - VladD