From Wikibooks, open books for an open world
The following subprograms are implementations of the Inventing an Algorithm examples.
The Ada example code does not append to the array as the algorithms. Instead we create an empty array of the desired length and then replace the characters inside.
functionTo_Lower (C : Character)returnCharacterrenamesAda.Characters.Handling.To_Lower; -- tolower - translates all alphabetic, uppercase characters -- in str to lowercasefunctionTo_Lower (Str : String)returnStringisResult : String (Str'Range);beginforCinStr'RangeloopResult (C) := To_Lower (Str (C));endloop;returnResult;endTo_Lower;
Would the append approach be impossible with Ada? No, but it would be significantly more complex and slower.
-- equal-ignore-case -- returns true if s or t are equal, -- ignoring casefunctionEqual_Ignore_Case (S : String; T : String)returnBooleanisO :constantInteger := S'First - T'First;beginifT'Length /= S'LengththenreturnFalse; -- if they aren't the same length, they -- aren't equalelseforIinS'RangeloopifTo_Lower (S (I)) /= To_Lower (T (I + O))thenreturnFalse;endif;endloop;endif;returnTrue;endEqual_Ignore_Case;
