My favorites
▼
|
Sign in
kpi-java-training-6
KPI Java Training fall 2013
Project Home
Wiki
Issues
Source
Export to GitHub
READ-ONLY: This project has been
archived
. For more information see
this post
.
Search
Search within:
All issues
Open issues
New issues
Issues to verify
for
Advanced search
Search tips
Subscriptions
Issue
11
attachment: Test1Palindromi.java
(2.8 KB)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package test1palindromi;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Test1Palindromi {
public static void main(String[] args) throws IOException {
// Чтение из командной строки
BufferedReader input = new BufferedReader( new InputStreamReader(System.in));
System.out.println("Enter the String:");
String str = input.readLine();
// Перевод строки в нижний реестр дабы исключить
// влияние последнего на сравнение символов
String str1 = str.toLowerCase();
String str2 = str1;
int pcount = 0; // Счетчик количества палиндромов
// Сравнение символов
label3: for ( int i = 1; i < str1.length(); i++){
// В первом внутреннем цикле будет производится поиск палиндромов
// не кратной 2м длинны те. 3, 5, ...
label1: for ( int a = i, b = i; a >= 0 && b < str1.length(); a--, b++){
// Сравнение символов по которым и будет приниматся решение
// является подстрока палиндромом или же нет
if ((char)str1.charAt(a) == (char)str1.charAt(b)){
// "Выделение" подстроки с палиндромом
String palindr = str.substring(a, b+1);
// Отсекание всех палиндромов длинной меньше 2х
if (palindr.length() > 2){
pcount++;
System.out.println("There is anothe Palindrome:\t" + palindr);
continue label1;
}
} else { break;
}
}
// Во втором внутреннем цикле производится поиск палиндромов
// длинной кратной двум ...
label2: for ( int a = i, b = i+1; a >= 0 && b < str2.length(); a--, b++){
if ((char)str2.charAt(a) == (char)str2.charAt(b)){
String palindr = str.substring(a, b+1);
if (palindr.length() > 2){
pcount++;
System.out.println("There is anothe Palindrome:\t" + palindr);
continue label2;
}
} else {
continue label3;
}
}
}
// Если счетчик палиндромов "пуст" это обозначает что
// их в строке не имеется.
if ( pcount == 0) {
System.out.println("There is no Palindromes!");
}
}
}
Powered by
Google Project Hosting