edited by
1,261 views
5 votes
5 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

  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^{\text{th}}$ through $j^{\text{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 by

2 Answers

Best answer
13 votes
13 votes

This function is reversing the string "HTGATE". 

Here, function substring() gives the substring from the $2^{\text{nd}}$ 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, $1^{\text{st}}$ 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

edited by

Related questions

19 votes
19 votes
3 answers
1
makhdoom ghaya asked Dec 15, 2016
4,158 views
Find a solution to the following recurrence equation:$T(n)=\sqrt{n}+T\left(\frac{n}{2}\right)$$T(1)=1$
7 votes
7 votes
2 answers
2
makhdoom ghaya asked Dec 15, 2016
2,596 views
A language uses an alphabet of six letters, $\left\{a, b, c, d, e, f\right\}$. The relative frequency of use of each letter of the alphabet in the language is as given be...
12 votes
12 votes
4 answers
4
makhdoom ghaya asked Nov 30, 2016
2,655 views
In the graph shown above, the depth-first spanning tree edges are marked with a $’ T’$. Identify the forward, backward, and cross edges.