C Programming · Question 23 · Medium

What will be the output of the following C program?

#include <stdio.h>
#include <string.h>

int main(void) {
    char s[] = "NAGPUR";
    int n = (int)strlen(s);
    for (int i = 0; i < n / 2; i++) {
        char t = s[i];
        s[i] = s[n - 1 - i];
        s[n - 1 - i] = t;
    }
    printf("%s", s);
    return 0;
}
  1. RUPGAN
  2. RUPNAG
  3. RAGPUN
  4. NAGPUR
Answer and explanation

Answer: A. RUPGAN

Explanation: The loop swaps s[i] with s[n - 1 - i] for i = 0, 1, 2, which reverses the 6-character string to RUPGAN.

Previous: What will be the output of the following C program? · Next: What will be the output of the following C program?

Practise c programming

Try a fresh set of c programming questions with an explanation after every answer, free and without signing up: C Programming practice. Or sit a full placement mock test.

More c programming questions

More technical & programming practice

Java · Python · Data Structures · Algorithms · Object-Oriented Programming · DBMS · SQL · Operating Systems

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge