-
Notifications
You must be signed in to change notification settings - Fork 153
/
Copy pathStringReversal.java
71 lines (64 loc) · 2.16 KB
/
StringReversal.java
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
60
61
62
63
64
65
66
67
68
69
70
71
/* @date 03/10/2020
* @author Prabhat Malhotra
*/
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
/*
* Solution A - String Reversal
*/
public class StringReversal {
public String stringReversal(String randomString) {
byte[] randomStringAsByteArray = randomString.getBytes();
byte[] byteArrayResult = new byte[randomStringAsByteArray.length];
for (int i = 0; i < byteArrayResult.length; i++) {
byteArrayResult[i] = randomStringAsByteArray[randomStringAsByteArray.length - i - 1];
}
return new String(byteArrayResult);
}
}
/*
* Test A - String Reversal
*/
class StringReversalTest {
@Test
public void reverseString() {
String randomString = "prabhat";
String expectedString = "tahbarp";
StringReversal stringReversal = new StringReversal();
System.out.println("The reverse of string " + randomString + " is " + stringReversal.stringReversal(randomString));
assertEquals(expectedString,stringReversal.stringReversal(randomString));
}
}
/*
* Solution B - Palindrome Check
*/
class PalindromeCheck {
public boolean palindromeChek(String randomString) {
StringReversal stringReversal = new StringReversal();
String reversedString = stringReversal.stringReversal(randomString);
return reversedString.equals(randomString);
}
}
/*
* Test B - Palindrome Check
*/
class PalindromeCheckTest {
@Test
public void shouldBePalindrome() {
String randomString = "LOL";
PalindromeCheck palindromeCheck = new PalindromeCheck();
if (palindromeCheck.palindromeChek(randomString)) {
System.out.println(randomString + " is a palindrome");
}
assertTrue(palindromeCheck.palindromeChek(randomString));
}
@Test
public void shouldNotToBePalindrome() {
String randomString = "HOT";
PalindromeCheck palindromeCheck = new PalindromeCheck();
if (!palindromeCheck.palindromeChek(randomString)) {
System.out.println(randomString + " is not a palindrome");
}
assertFalse(palindromeCheck.palindromeChek(randomString));
}
}