0 votes

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

- type string=record

length:integer;

C:array[1..100] of char

end - 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$.
- 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}$.

5 votes

Best answer

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**