Txt-Reverse string

From Vectorlab
Jump to: navigation, search


Function to reverse the sequence of chars in a string. By Orso B. Schmid


{ Orso *********************************************** }
{ reverse char order of a string }
FUNCTION T_ReverseStr(str: STRING): STRING;
	VAR
		temp_dc, reversed : DYNARRAY[] OF CHAR;
		n, m : INTEGER;
		
	BEGIN
		IF Len(str) > 0 THEN BEGIN
			temp_dc := str;
			reversed := str; { this inits the length of the array }
			
			{ reverse the name in order to have Pos identify the eventual number }
			m := 1;
			n := Len(temp_dc);
			
			REPEAT
				reversed[m] := temp_dc[n];
				n := n - 1;
				m := m + 1;
			UNTIL n = 0;
			
			T_ReverseStr := reversed;
			
		END ELSE
			T_ReverseStr := str;
	END;