The Gateway to Computer Science Excellence
0 votes
229 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}$.
in Algorithms by Boss (30.7k points)
edited by | 229 views

2 Answers

+3 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

by Boss (17k points)
selected by
0
A great full simulation.
0 votes
TGATET
by Active (2.7k points)
0
explain plz

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
50,737 questions
57,279 answers
198,173 comments
104,840 users