R2.23 Suppose you are given a string str and two positions i and j, where i come
ID: 3869843 • Letter: R
Question
R2.23 Suppose you are given a string str and two positions i and j, where i comes before j. The following pseudocode describes how to swap two letters in a word.
We are given a string str and two positions i and j. (i comes before j)
Set first to the substring from the start of the string to the last position before i.
Set middle to the substring from positions i + 1 to j - 1.
Set last to the substring from position j + 1 to the end of the string.
Concatenate the following five strings: first, the string containing just the character at position j, middle, the string containing just the character at position i, and last.
Check this pseudocode, using the string "Gateway" and positions 2 and 4. Draw a diagram of the string that is being computed, similar to Figure 5.
Explanation / Answer
Please find my answer.
Given String: Gateway
i = 2, j = 4
We are given a string str and two positions i and j. (i comes before j)
Set first to the substring from the start of the string to the last position before i.
first = Ga
Set middle to the substring from positions i + 1 to j - 1.
middle = e
Set last to the substring from position j + 1 to the end of the string.
last = ay
Concatenate the following five strings: first, the string containing just the character at position j,
middle, the string containing just the character at position i, and last.
first + "w" + middle + "t" + last
= Gawetay
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.