The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
0 votes
160 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
     (i) type string=record
                      length:integer;
                      C:array[1..100] of char
                      end

(ii) 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$.

(iii) 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}$.

asked in Algorithms by Boss (29.3k points) | 160 views

1 Answer

0 votes
TGATET
answered by Active (2.8k points)
0
explain plz
0

I think this function is reversing the string "HTGATE". I don't know the syntax of the given language but it looks like it is doing reversing.

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

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
49,587 questions
54,197 answers
187,535 comments
71,151 users