C Program to Reverse an Array or String
C Program to Reverse an Array or String
A school text exercise needs to reverse arrays and strings while preserving the buffer’s valid characters and final terminator.
Reverse a bounded sequence by swapping symmetric elements through half its logical length, leaving a string’s terminator outside the swapped range.
Before this exercise, review strings and their terminator and array bounds. The worked program below applies those ideas to this specific problem.
Explain
Explain why reverse arrays and strings uses this sequence of operations.
Trace
Trace the sample values and predict the printed result.
Apply
Reverse Ravi in place while preserving the final null character.
Reverse Arrays and Strings: follow the data
Given data
A school text exercise needs to reverse arrays and strings while preserving the buffer’s valid characters and final terminator.
Rule
Reverse a bounded sequence by swapping symmetric elements through half its logical length, leaving a string’s terminator outside the swapped range.
Observed result
ahsA Marks: 45 35 40
Understand the operation
The same positional idea applies to char and int elements, but their temporary variables and bounds must match their types. A string’s visible length excludes its terminator.
A C string ends at its first null character. Read only within its accessible storage and reserve a position for that terminator when producing a changed string. These examples use basic text bytes, not a Unicode grapheme-processing library.
Read the complete C program
The input values are written into this complete C17 example so the calculation can be reproduced without an unavailable service. Change the named data, rebuild and compare the actual output with your prediction.
C17 · complete program
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(void) {
char text[] = "Asha";
size_t length = strlen(text);
for (size_t i = 0; i < length / 2; i++) { char saved = text[i]; text[i] = text[length - 1 - i]; text[length - 1 - i] = saved; }
puts(text);
int marks[] = {40,35,45};
for (size_t i = 0; i < 3 / 2; i++) { int saved = marks[i]; marks[i] = marks[2 - i]; marks[2 - i] = saved; }
printf("Marks: %d %d %d\n", marks[0], marks[1], marks[2]);
return 0;
}
Expected output
ahsA
Marks: 45 35 40
Run in C compiler →Follow the values through the code
C17 · code used in this explanation
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(void) {
char text[] = "Asha";
size_t length = strlen(text);
for (size_t i = 0; i < length / 2; i++) { char saved = text[i]; text[i] = text[length - 1 - i]; text[length - 1 - i] = saved; }
puts(text);
int marks[] = {40,35,45};
for (size_t i = 0; i < 3 / 2; i++) { int saved = marks[i]; marks[i] = marks[2 - i]; marks[2 - i] = saved; }
printf("Marks: %d %d %d\n", marks[0], marks[1], marks[2]);
return 0;
}
Asha swaps its A/a and s/h pairs to produce ahsA. The three-mark sequence exchanges its endpoints while preserving its middle value.
Compare the printed answer with the original text and the stated ordering or filtering rule. The presence of a terminator makes %s usable; it does not by itself make every earlier write fit.
Follow the worked calculation
Start with the sample
Asha swaps its A/a and s/h pairs to produce ahsA. The three-mark sequence exchanges its endpoints while preserving its middle value.
Apply the rule
Compare the printed answer with the original text and the stated ordering or filtering rule. The presence of a terminator makes %s usable; it does not by itself make every earlier write fit.
Check the result
ahsA Marks: 45 35 40
ahsA Marks: 45 35 40
Try a small variation
C17 · code used in this explanation
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(void) {
char text[] = "Asha";
size_t length = strlen(text);
for (size_t i = 0; i < length / 2; i++) { char saved = text[i]; text[i] = text[length - 1 - i]; text[length - 1 - i] = saved; }
puts(text);
int marks[] = {40,35,45};
for (size_t i = 0; i < 3 / 2; i++) { int saved = marks[i]; marks[i] = marks[2 - i]; marks[2 - i] = saved; }
printf("Marks: %d %d %d\n", marks[0], marks[1], marks[2]);
return 0;
}
Reverse Ravi in place while preserving the final null character.
Worked answer
The complete worked answer applies the same explicitly bounded operation to a small variation. Read its stored bytes and terminator separately from the visible output.
C17 · complete program
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(void) {
char text[] = "Ravi";
size_t length = strlen(text);
for (size_t i = 0; i < length / 2; i++) { char saved = text[i]; text[i] = text[length - 1 - i]; text[length - 1 - i] = saved; }
puts(text);
return 0;
}
Expected output
ivaR
Run in C compiler →Recognize the important boundaries
Swapping through the full length would undo the reversal. An empty string must not form a length−1 access outside a loop that excludes it.
An untrusted input string needs a verified buffer bound and a valid terminator. strlen is not a safe way to prove arbitrary raw storage has a terminator.
Check your understanding
Choose the answer, then compare the reasoning for every option.
Use the idea independently
Reverse a bounded sequence by swapping symmetric elements through half its logical length, leaving a string’s terminator outside the swapped range.
Continue with character-array storage.
Continue with bounded line reading.
Language reference: WG14 C11 committee draft. These examples use the C17-compatible subset of the explained language features.
Reaching the recap and completing the quizzes are separate milestones.