Consider the C/C++ function f() given below:
void f(char w[])
{
int x = strlen(w); // length of the string
char c;
for (int i = 0; i < x; i++)
{
c = w[i];
w[i] = w[x − i − 1];
w[x − i − 1] = c;
}
}Which of the following is the purpose of f()?
Pick an option to check yourself, or open the answer below.
Show answer & explanation
Correct answer: B. It outputs the contents of the array in the original order.
Explanation
The loop runs over the whole string, so each pair is swapped twice (once at i and again at x − i − 1). The string ends up in its original order.
More Programming Languages and Computer Graphics questions
- The definitions in an XML document are said to be __________ when the tagging system and definitions in the DTD are all in compliance.
- Consider the JavaScript code: var y = "12"; function f() { var y = "6"; alert(this.y); function g() { alert(y); } g(); } f(); If M is the n…
- What is the output of the following Java program? class simple { public static void main(String[] args) { simple obj = new simple(); obj.st…
- What is the output of the following C program? (Assume little-endian representation, with the least significant byte at the lowest address,…
- Given below are three implementations of swap() in C++: (a) void swap(int a, int b) { int temp; temp = a; a = b; b = temp; } called as swap…
- In Java, which of the following statements is/are True ? S1 : The ‘final’ keyword applied to a class definition prevents the class from bei…