Go String Slicer and Reverser
This challenge will test your ability to manipulate strings in Go by implementing two core functionalities: slicing a string based on provided indices and reversing a given string. Mastering these operations is fundamental for various text processing tasks in Go.
Problem Description
You are tasked with creating a Go program that can perform two distinct string manipulations:
- Slice a String: Given an input string and two integer indices (start and end), extract the substring between these indices. The start index is inclusive, and the end index is exclusive, following standard Go slicing conventions.
- Reverse a String: Given an input string, return a new string with the characters in reverse order.
Your program should encapsulate these functionalities within a single function.
Key Requirements:
- The function should accept a string and two integer parameters for slicing.
- The function should also be able to reverse the entire string if specific (or default) indicators are provided for the slice indices.
- Handle edge cases gracefully.
Expected Behavior:
- When valid start and end indices are provided, the function should return the corresponding substring.
- If specific values are provided for start and end (e.g.,
-1for start and-2for end) to signal a reversal, the function should return the reversed string. - If the input string is empty, the function should return an empty string.
Edge Cases to Consider:
- Empty input string.
- Start index greater than end index.
- Indices out of bounds (e.g., negative indices beyond the reversal signal, or indices exceeding string length).
- The specific values used to trigger string reversal.
Examples
Example 1:
Input: str = "HelloWorld", start = 0, end = 5
Output: "Hello"
Explanation: Extracts the substring from index 0 (inclusive) up to index 5 (exclusive).
Example 2:
Input: str = "Programming", start = -1, end = -2
Output: "gnimmargorP"
Explanation: The special values -1 and -2 indicate string reversal. The input string "Programming" is reversed.
Example 3:
Input: str = "GoLang", start = 2, end = 4
Output: "La"
Explanation: Extracts the substring from index 2 (inclusive) up to index 4 (exclusive).
Example 4:
Input: str = "", start = 0, end = 1
Output: ""
Explanation: An empty input string should result in an empty output string, regardless of indices.
Example 5:
Input: str = "Test", start = 4, end = 2
Output: ""
Explanation: When start index is greater than end index, an empty string is returned.
Constraints
- The input string will consist of printable ASCII characters.
- The length of the input string will not exceed 1000 characters.
- The
startandendindices will be integers within the range of -2 to 1000. - The special values
-1forstartand-2forendare exclusively used to signal string reversal.
Notes
- Consider how you will handle potential panics from out-of-bounds slicing in Go.
- Think about efficient ways to reverse a string in Go.
- The function should return a
string. - The implementation should be a single function, likely named
ManipulateString.