ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

KQL: Reverse a String

Kusto Query Language (KQL)

Goal -- WPM

Ready
Exercise Algorithm Area
1let reverseString = (inputString: string) -> string {
2let reversed = "";
3let len = strlen(inputString);
4
5// Handle empty string case
6if (len == 0) {
7return "";
8}
9
10// Iterate from the last character to the first
11for (let i = len - 1; i >= 0; i--) {
12// Append the current character to the reversed string
13reversed = strcat(reversed, substring(inputString, i, 1));
14}
15
16return reversed;
17};
18
19// Example Usage:
20print reverseString("hello"); // Expected: "olleh"
21print reverseString("Kusto"); // Expected: "otsuK"
22print reverseString("a"); // Expected: "a"
23print reverseString(""); // Expected: ""
24print reverseString("madam"); // Expected: "madam"
Algorithm description viewbox

KQL: Reverse a String

Algorithm description:

This KQL function reverses a given string. It iterates through the input string from the last character to the first, appending each character to a new string. This is a fundamental string manipulation technique with applications in data processing, text analysis, and cryptography where reversing text might be a required step. For example, it could be used to check for palindromes or to prepare text for certain encoding schemes.

Algorithm explanation:

The `reverseString` function takes a string `inputString` and returns its reversed version. It initializes an empty string `reversed`. It then gets the length of the input string. If the string is empty, it returns an empty string immediately. Otherwise, it iterates from the last character of the input string (index `len - 1`) down to the first character (index 0). In each iteration, it takes the character at the current index `i` using `substring` and appends it to the `reversed` string using `strcat`. After the loop completes, the `reversed` string holds the reversed version of the original string, which is then returned. The time complexity is O(N), where N is the length of the string, because each character is processed once for appending. The space complexity is O(N) to store the new reversed string.

Pseudocode:

function reverseString(inputString):
  reversed = ""
  len = length of inputString

  if len == 0:
    return ""

  for i from len - 1 down to 0:
    reversed = reversed + inputString[i]

  return reversed