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

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

Ada String Reversal with Helper

Ada

Goal -- WPM

Ready
Exercise Algorithm Area
1with Ada.Text_IO;
2use Ada.Text_IO;
3with Ada.Strings.Unbounded;
4use Ada.Strings.Unbounded;
5
6procedure String_Reversal is
7
8-- Helper procedure to swap two characters in a string
9procedure Swap_Chars (S : in out Unbounded_String; Index1, Index2 : Natural) is
10begin
11-- Ensure indices are valid before swapping
12if Index1 >= 1 and Index1 <= Length(S) and
13Index2 >= 1 and Index2 <= Length(S) then
14declare
15Temp : Character;
16begin
17Temp := S(Index1);
18S(Index1) := S(Index2);
19S(Index2) := Temp;
20end;
21end if;
22end Swap_Chars;
23
24-- Main procedure to reverse a string
25procedure Reverse_String (S : in out Unbounded_String) is
26Len : Natural;
27Mid : Natural;
28begin
29Len := Length(S);
30
31-- Handle edge cases: empty or single-character string
32if Len <= 1 then
33return; -- Nothing to reverse
34end if;
35
36-- Calculate the midpoint for swapping
37Mid := Len / 2;
38
39-- Iterate from the beginning up to the midpoint, swapping characters
40for I in 1 .. Mid loop
41Swap_Chars(S, I, Len - I + 1);
42end loop;
43end Reverse_String;
44
45-- Example Usage
46My_String : Unbounded_String := To_Unbounded_String("Hello, Ada!");
47Empty_String : Unbounded_String := To_Unbounded_String("");
48Single_Char_String : Unbounded_String := To_Unbounded_String("A");
49
50begin
51Put_Line("Original string: " & To_String(My_String));
52Reverse_String(My_String);
53Put_Line("Reversed string: " & To_String(My_String));
54
55Put_Line("Original empty string: " & To_String(Empty_String));
56Reverse_String(Empty_String);
57Put_Line("Reversed empty string: " & To_String(Empty_String));
58
59Put_Line("Original single char string: " & To_String(Single_Char_String));
60Reverse_String(Single_Char_String);
61Put_Line("Reversed single char string: " & To_String(Single_Char_String));
62
63end String_Reversal;
Algorithm description viewbox

Ada String Reversal with Helper

Algorithm description:

This Ada program demonstrates how to reverse a string using a helper procedure for swapping characters. The `Reverse_String` procedure iterates through the first half of the string, swapping each character with its corresponding character from the end. This is a common algorithm used in various programming tasks, such as preparing data for comparison or implementing palindrome checks.

Algorithm explanation:

The `String_Reversal` procedure reverses an `Unbounded_String` in place. It utilizes a helper procedure `Swap_Chars` to exchange characters at specified indices. The main `Reverse_String` procedure calculates the length and midpoint of the string. It then iterates from the first character up to the midpoint, calling `Swap_Chars` to swap the character at index `I` with the character at index `Len - I + 1`. This ensures that characters from the beginning are swapped with their counterparts from the end, effectively reversing the string. The time complexity is O(N), where N is the length of the string, because each character is involved in at most one swap. The space complexity is O(1) as it modifies the string in place without using significant extra memory. Edge cases like empty strings or single-character strings are handled by returning early, as no reversal is needed.

Pseudocode:

PROCEDURE Reverse_String(S):
  LEN = length of S
  IF LEN <= 1 THEN
    RETURN
  END IF

  MID = LEN / 2
  FOR I FROM 1 TO MID:
    SWAP_CHARS(S, I, LEN - I + 1)
  END FOR
END PROCEDURE

PROCEDURE Swap_Chars(S, Index1, Index2):
  IF Index1 and Index2 are valid indices in S THEN
    TEMP = S[Index1]
    S[Index1] = S[Index2]
    S[Index2] = TEMP
  END IF
END PROCEDURE