175 views

What is the output produced by the following program, when the input is "HTGATE"

Function what (s:string): string;
var n:integer;
begin
n = s.length
if n <= 1
then what := s
else what :=contact (what (substring (s, 2, n)), s.C [1])
end;


Note

1. type string=record
length:integer;
C:array[1..100] of char
end
2. Substring (s, i, j): this yields the string made up of the $i^{th}$ through $j^{th}$ characters in s; for appropriately defined in $i$ and $j$.
3. Contact $(s_{1}, s_{2})$: this function yields a string of length $s_{1}$ length + $s_{2}$ - length obtained by concatenating $s_{1}$ with $s_{2}$ such that $s_{1}$ precedes $s_{2}$.

edited | 175 views

This function is reversing the string "HTGATE".

Here, function substring() gives the substring from the 2nd character of the original string till the end and contact(p,q) is concatenating the strings p and q...

n = s.length means n = length of the string. and c[1] means 1st character of the passed string

So, it will work like this:

what(HTGATE)

contact(what(TGATE),H)

contact(contact(what(GATE),T),H)

contact(contact(contact(what(ATE),G),T),H)

contact(contact(contact(contact(what(TE),A),G),T),H)

contact(contact(contact(contact(contact(what(E),T),A),G),T),H)

contact(contact(contact(contact(contact(E,T),A),G),T),H)

contact(contact(contact(contact(ET,A),G),T),H)

contact(contact(contact(ETA,G),T),H)

contact(contact(ETAG,T),H)

contact(ETAGT,H)

ETAGTH

by Boss (14.4k points)
selected by
TGATET
by Active (2.8k points)
0
explain plz

1
2