C Program to Sort a String
C Program to Sort a String
A school text exercise needs to sort string bytes while preserving the buffer’s valid characters and final terminator.
A byte-value string sort orders stored characters by their execution byte values; it is not automatically a locale-aware dictionary ordering.
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 sort string bytes uses this sequence of operations.
Trace
Trace the sample values and predict the printed result.
Apply
Sort baba and explain why duplicates remain in the result.
Sort String Bytes: follow the data
Given data
A school text exercise needs to sort string bytes while preserving the buffer’s valid characters and final terminator.
Rule
A byte-value string sort orders stored characters by their execution byte values; it is not automatically a locale-aware dictionary ordering.
Observed result
abcd
Understand the operation
This insertion loop moves greater unsigned-char values right and inserts a saved character. The string terminator remains beyond the sorted visible range.
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[] = "dcba";
size_t length = strlen(text);
for (size_t i = 1; i < length; i++) {
char value = text[i]; size_t j = i;
while (j > 0 && (unsigned char)text[j - 1] > (unsigned char)value) { text[j] = text[j - 1]; j--; }
text[j] = value;
}
puts(text);
return 0;
}
Expected output
abcd
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[] = "dcba";
size_t length = strlen(text);
for (size_t i = 1; i < length; i++) {
char value = text[i]; size_t j = i;
while (j > 0 && (unsigned char)text[j - 1] > (unsigned char)value) { text[j] = text[j - 1]; j--; }
text[j] = value;
}
puts(text);
return 0;
}
On the ASCII-compatible verification host, d,c,b,a insert into a,b,c,d.
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
On the ASCII-compatible verification host, d,c,b,a insert into a,b,c,d.
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
abcd
abcd
Try a small variation
C17 · code used in this explanation
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(void) {
char text[] = "dcba";
size_t length = strlen(text);
for (size_t i = 1; i < length; i++) {
char value = text[i]; size_t j = i;
while (j > 0 && (unsigned char)text[j - 1] > (unsigned char)value) { text[j] = text[j - 1]; j--; }
text[j] = value;
}
puts(text);
return 0;
}
Sort baba and explain why duplicates remain in the result.
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[] = "baba";
size_t length = strlen(text);
for (size_t i = 1; i < length; i++) {
char value = text[i]; size_t j = i;
while (j > 0 && (unsigned char)text[j - 1] > (unsigned char)value) { text[j] = text[j - 1]; j--; }
text[j] = value;
}
puts(text);
return 0;
}
Expected output
aabb
Run in C compiler →Recognize the important boundaries
Dictionary order, case folding and Unicode collation need a defined text comparison beyond this byte-value demonstration.
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
A byte-value string sort orders stored characters by their execution byte values; it is not automatically a locale-aware dictionary ordering.
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.