Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit04dc9af

Browse files
committed
Add palindrome check snippet
1 parent2457472 commit04dc9af

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

‎README.md‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Update the sample application with the snippet and add a test for it. After prov
2525
*[Capture screen](#capture-screen)
2626

2727
###String
28+
*[Palindrome check](#palindrome-check)
2829
*[Reverse string](#reverse-string)
2930
*[String to date](#string-to-date)
3031

@@ -129,6 +130,24 @@ Update the sample application with the snippet and add a test for it. After prov
129130

130131
##String
131132

133+
###Palindrome check
134+
135+
```java
136+
publicstaticboolean isPalindrome(String s) {
137+
StringBuilder sb=newStringBuilder();
138+
for (char c: s.toCharArray()) {
139+
if (Character.isLetter(c)) {
140+
sb.append(c);
141+
}
142+
}
143+
String forward= sb.toString().toLowerCase();
144+
String backward= sb.reverse().toString().toLowerCase();
145+
return forward.equals(backward);
146+
}
147+
```
148+
149+
[⬆ back to top](#table-of-contents)
150+
132151
###Reverse string
133152

134153
```java

‎src/main/java/Library.java‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,23 @@ public static Date stringToDate(String date, String format) throws ParseExceptio
130130
publicstaticFile[]listDirectories(Stringpath) {
131131
returnnewFile(path).listFiles(File::isDirectory);
132132
}
133+
134+
/**
135+
* Checks if given string is palindrome (same forward and backward)
136+
* Skips non-letter characters
137+
* Credits: https://github.com/kousen/java_8_recipes
138+
* @param s string to check
139+
* @return true if palindrome
140+
*/
141+
publicstaticbooleanisPalindrome(Strings) {
142+
StringBuildersb =newStringBuilder();
143+
for (charc :s.toCharArray()) {
144+
if (Character.isLetter(c)) {
145+
sb.append(c);
146+
}
147+
}
148+
Stringforward =sb.toString().toLowerCase();
149+
Stringbackward =sb.reverse().toString().toLowerCase();
150+
returnforward.equals(backward);
151+
}
133152
}

‎src/test/java/LibraryTest.java‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,4 +151,15 @@ public void testListDirectories() {
151151
assertTrue(Arrays.stream(files).anyMatch(newFile("src/test/resources/dir1")::equals));
152152
assertTrue(Arrays.stream(files).anyMatch(newFile("src/test/resources/dir2")::equals));
153153
}
154+
155+
/**
156+
* Tests for {@link Library#isPalindrome(String)}
157+
*/
158+
@Test
159+
publicvoidtestIsPalindrome() {
160+
assertTrue(Library.isPalindrome("saippuakauppias"));
161+
assertTrue(Library.isPalindrome("111 Saippua - Kauppias 321"));
162+
assertFalse(Library.isPalindrome("Type O Negative"));
163+
assertFalse(Library.isPalindrome("Foo12121Bar"));
164+
}
154165
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp