Skip to content

Commit 6356db0

Browse files
* rename file
* create strings directory * fix docs * add test
1 parent f90a8be commit 6356db0

File tree

7 files changed

+148
-121
lines changed

7 files changed

+148
-121
lines changed

Others/Armstrong.java renamed to Maths/Armstrong.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package Others;
1+
package strings;
22

33
/**
44
* An Armstrong number is equal to the sum of the cubes of its digits.

Others/Abecedarian.java

Lines changed: 0 additions & 24 deletions
This file was deleted.

Others/Palindrome.java

Lines changed: 0 additions & 50 deletions
This file was deleted.

Others/ReverseString.java

Lines changed: 0 additions & 46 deletions
This file was deleted.

strings/Alphabetical.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package strings;
2+
3+
/**
4+
* <p>
5+
* Alphabetical order is a system whereby character strings are placed in order
6+
* based on the position of the characters in the conventional ordering of an alphabet.
7+
* </p>
8+
* Wikipedia: https://en.wikipedia.org/wiki/Alphabetical_order
9+
*/
10+
class Alphabetical {
11+
12+
public static void main(String[] args) {
13+
assert !isAlphabetical("123abc");
14+
assert isAlphabetical("aBC");
15+
assert isAlphabetical("abc");
16+
assert !isAlphabetical("xyzabc");
17+
assert isAlphabetical("abcxyz");
18+
}
19+
20+
/**
21+
* Check if a string is alphabetical order or not
22+
*
23+
* @param s a string
24+
* @return {@code true} if given string is alphabetical order, otherwise {@code false}
25+
*/
26+
public static boolean isAlphabetical(String s) {
27+
s = s.toLowerCase();
28+
for (int i = 0; i < s.length() - 1; ++i) {
29+
if (!Character.isLetter(s.charAt(i)) || !(s.charAt(i) <= s.charAt(i + 1))) {
30+
return false;
31+
}
32+
}
33+
return true;
34+
}
35+
}

strings/Palindrome.java

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package strings;
2+
3+
/**
4+
* Wikipedia: https://en.wikipedia.org/wiki/Palindrome
5+
*/
6+
class Palindrome {
7+
8+
/**
9+
* Driver Code
10+
*/
11+
public static void main(String[] args) {
12+
String[] palindromes = {null, "", "aba", "123321"};
13+
for (String s : palindromes) {
14+
assert isPalindrome(s) && isPalindromeRecursion(s) && isPalindrome1(s);
15+
}
16+
17+
String[] notPalindromes = {"abb", "abc", "abc123"};
18+
for (String s : notPalindromes) {
19+
assert !isPalindrome(s) && !isPalindromeRecursion(s) && !isPalindrome1(s);
20+
}
21+
}
22+
23+
/**
24+
* Check if a string is palindrome string or not
25+
*
26+
* @param s a string to check
27+
* @return {@code true} if given string is palindrome, otherwise {@code false}
28+
*/
29+
public static boolean isPalindrome(String s) {
30+
return (s == null || s.length() <= 1) || s.equals(new StringBuilder(s).reverse().toString());
31+
}
32+
33+
/**
34+
* Check if a string is palindrome string or not using recursion
35+
*
36+
* @param s a string to check
37+
* @return {@code true} if given string is palindrome, otherwise {@code false}
38+
*/
39+
public static boolean isPalindromeRecursion(String s) {
40+
if (s == null || s.length() <= 1) {
41+
return true;
42+
}
43+
44+
if (s.charAt(0) != s.charAt(s.length() - 1)) {
45+
return false;
46+
}
47+
48+
return isPalindrome(s.substring(1, s.length() - 1));
49+
}
50+
51+
/**
52+
* Check if a string is palindrome string or not another way
53+
*
54+
* @param s a string to check
55+
* @return {@code true} if given string is palindrome, otherwise {@code false}
56+
*/
57+
public static boolean isPalindrome1(String s) {
58+
if (s == null || s.length() <= 1) {
59+
return true;
60+
}
61+
for (int i = 0, j = s.length() - 1; i < j; ++i, --j) {
62+
if (s.charAt(i) != s.charAt(j)) {
63+
return false;
64+
}
65+
}
66+
return true;
67+
}
68+
}

strings/ReverseString.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package strings;
2+
3+
/**
4+
* Reverse String using different version
5+
*/
6+
public class ReverseString {
7+
8+
public static void main(String[] args) {
9+
assert reverse("abc123").equals("321cba");
10+
assert reverse2("abc123").equals("321cba");
11+
}
12+
13+
/**
14+
* easiest way to reverses the string str and returns it
15+
*
16+
* @param str string to be reversed
17+
* @return reversed string
18+
*/
19+
public static String reverse(String str) {
20+
return new StringBuilder(str).reverse().toString();
21+
}
22+
23+
/**
24+
* second way to reverses the string str and returns it
25+
*
26+
* @param str string to be reversed
27+
* @return reversed string
28+
*/
29+
public static String reverse2(String str) {
30+
31+
if (str == null || str.isEmpty()) {
32+
return str;
33+
}
34+
35+
char[] value = str.toCharArray();
36+
for (int i = 0, j = str.length() - 1; i < j; i++, j--) {
37+
char temp = value[i];
38+
value[i] = value[j];
39+
value[j] = temp;
40+
}
41+
return new String(value);
42+
}
43+
44+
}

0 commit comments

Comments
 (0)