From 173a7297d1355ea8d38f075e7236ef68be84eebd Mon Sep 17 00:00:00 2001 From: = Date: Tue, 17 Oct 2023 00:10:53 +0530 Subject: [PATCH 1/2] Added ReverseString --- src/main/kotlin/strings/ReverseString.kt | 32 ++++++++++++++++++ src/test/kotlin/strings/ReverseStringTest.kt | 35 ++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 src/main/kotlin/strings/ReverseString.kt create mode 100644 src/test/kotlin/strings/ReverseStringTest.kt diff --git a/src/main/kotlin/strings/ReverseString.kt b/src/main/kotlin/strings/ReverseString.kt new file mode 100644 index 0000000..8a0c5dc --- /dev/null +++ b/src/main/kotlin/strings/ReverseString.kt @@ -0,0 +1,32 @@ +package strings + +/** + * Reverses a given string in-place. + * + * @param str The input string to be reversed. + * @return The reversed string. + */ +fun reverseString(str: String): String { + // Check if the input string is null or empty. + if (str.isNullOrEmpty()) { + // If it is, return the input string as it is (no change needed). + return str + } + + // Convert the input string to a character array. + val charArray = str.toCharArray() + var i = 0 + var j = str.length - 1 + + // Swap characters from the beginning and end to reverse the string in-place. + while (i < j) { + val temp = charArray[i] + charArray[i] = charArray[j] + charArray[j] = temp + i++ + j-- + } + + // Convert the character array back to a string and return the reversed string. + return String(charArray) +} diff --git a/src/test/kotlin/strings/ReverseStringTest.kt b/src/test/kotlin/strings/ReverseStringTest.kt new file mode 100644 index 0000000..944f102 --- /dev/null +++ b/src/test/kotlin/strings/ReverseStringTest.kt @@ -0,0 +1,35 @@ +package strings + +import org.junit.Assert.assertEquals +import org.junit.Test + +class ReverseStringTest { + + @Test + fun testReverseStringWithEmptyString() { + val input = "" + val expected = "" + assertEquals(expected, reverseString(input)) + } + + @Test + fun testReverseStringWithSingleCharacter() { + val input = "a" + val expected = "a" + assertEquals(expected, reverseString(input)) + } + + @Test + fun testReverseStringWithEvenLengthString() { + val input = "abcdef" + val expected = "fedcba" + assertEquals(expected, reverseString(input)) + } + + @Test + fun testReverseStringWithOddLengthString() { + val input = "hello" + val expected = "olleh" + assertEquals(expected, reverseString(input)) + } +} From f6535ef9fc4114015746ef057620aac2364e3576 Mon Sep 17 00:00:00 2001 From: = Date: Tue, 17 Oct 2023 00:20:46 +0530 Subject: [PATCH 2/2] Added ReverseString --- src/main/kotlin/strings/ReverseString.kt | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/main/kotlin/strings/ReverseString.kt b/src/main/kotlin/strings/ReverseString.kt index 8a0c5dc..6de50ed 100644 --- a/src/main/kotlin/strings/ReverseString.kt +++ b/src/main/kotlin/strings/ReverseString.kt @@ -1,24 +1,20 @@ package strings /** - * Reverses a given string in-place. + * Reverses a given string * * @param str The input string to be reversed. * @return The reversed string. */ fun reverseString(str: String): String { - // Check if the input string is null or empty. if (str.isNullOrEmpty()) { - // If it is, return the input string as it is (no change needed). return str } - // Convert the input string to a character array. val charArray = str.toCharArray() var i = 0 var j = str.length - 1 - // Swap characters from the beginning and end to reverse the string in-place. while (i < j) { val temp = charArray[i] charArray[i] = charArray[j] @@ -27,6 +23,5 @@ fun reverseString(str: String): String { j-- } - // Convert the character array back to a string and return the reversed string. return String(charArray) }