Sorting names alphabetically in C means sorting an array of strings — and that’s what makes this program a classic: you can’t compare strings with > or swap them with =. You need strcmp() to compare and strcpy() to swap, and that pair of ideas is exactly what this exercise teaches. This version reads N names, bubble-sorts them, and prints the original and sorted lists side by side — in tested, warning-free C89, with the non-standard strcmpi() from the 2012 version replaced by a portable case-insensitive comparison you can actually compile today.
How It Works — Step by Step
- Read N, then N names into a 2-D char array
names[MAX_NAMES][NAME_LEN]— one row per name. A copy of the original order is kept for the side-by-side output. - Bubble sort the rows: compare adjacent names with
strcmp(names[j], names[j+1]). A positive return value means they’re out of order. - Swap out-of-order names with three
strcpy()calls through atempbuffer — whole strings move, not pointers. - After pass i, the “largest” remaining name has sunk to the end, so each pass scans one element fewer (
n - 1 - i).
How strcmp decides order: it walks both strings and returns the difference of the first differing characters. "Amit" vs "Bela": ‘A’ (65) − ‘B’ (66) = −1 → negative → already in order.
C Program to Sort Names in Alphabetical Order
#include <stdio.h>
#include <string.h>
#define MAX_NAMES 20
#define NAME_LEN 32
int main(void)
{
char names[MAX_NAMES][NAME_LEN];
char original[MAX_NAMES][NAME_LEN];
char temp[NAME_LEN];
int n, i, j;
printf("How many names? ");
if (scanf("%d", &n) != 1 || n < 1 || n > MAX_NAMES) {
printf("Invalid count.\n");
return 1;
}
printf("Enter %d names:\n", n);
for (i = 0; i < n; i++) {
if (scanf("%31s", names[i]) != 1) {
printf("Invalid input.\n");
return 1;
}
strcpy(original[i], names[i]);
}
/* bubble sort: after each pass the "largest" name sinks to the end */
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - 1 - i; j++) {
if (strcmp(names[j], names[j + 1]) > 0) {
strcpy(temp, names[j]);
strcpy(names[j], names[j + 1]);
strcpy(names[j + 1], temp);
}
}
}
printf("\n%-15s %-15s\n", "Input names", "Sorted names");
printf("------------------------------\n");
for (i = 0; i < n; i++) {
printf("%-15s %-15s\n", original[i], names[i]);
}
return 0;
}
How to Compile and Run
gcc -ansi -Wall -Wextra -o sortnames sortnames.c
./sortnames
Sample Input and Output
Test 1:
How many names? 5
Enter 5 names:
Ravi
Amit
Zara
John
Bela
Input names Sorted names
------------------------------
Ravi Amit
Amit Bela
Zara John
John Ravi
Bela Zara
Test 2 — mixed case exposes an important detail:
How many names? 3
Enter 3 names:
zara
Amit
ravi
Input names Sorted names
------------------------------
zara Amit
Amit ravi
ravi zara
strcmp() is case-sensitive: all uppercase letters (ASCII 65–90) sort before all lowercase ones (97–122), so Amit beats ravi beats zara. Often that’s what you want; when it isn’t, see below.
Case-Insensitive Sorting — the Portable Way
The 2012 version of this program used strcmpi() — a compiler-specific function that doesn’t exist in standard C (modern GCC and Clang won’t link it). The portable fix is a comparison that lowercases as it walks:
#include <ctype.h>
/* Case-insensitive comparison: strcmpi/stricmp are NOT standard C.
This portable version works with every compiler. */
static int compare_ignore_case(const char *a, const char *b)
{
while (*a != '\0' && *b != '\0') {
int ca = tolower((unsigned char)*a);
int cb = tolower((unsigned char)*b);
if (ca != cb) {
return ca - cb;
}
a++;
b++;
}
return tolower((unsigned char)*a) - tolower((unsigned char)*b);
}
Swap it in for strcmp in the sort loop and zara / Amit / ravi sorts as Amit ravi zara. The (unsigned char) cast matters: passing a plain (possibly negative) char to tolower() is undefined behavior.
Code Explanation
char names[20][32]— a 2-D array: 20 rows of 32 bytes; each row is one independent string. The 2012 version used rows of 8 bytes, which overflows on any name longer than 7 letters.scanf("%31s", ...)— the width limit stops input at 31 characters + terminator, preventing the buffer overflow that unbounded%sinvites.- Three
strcpy()calls per swap — strings can’t be assigned with=; array contents must be copied. (Sorting an array of pointers instead, and swapping just the pointers, is the faster follow-up — see the related posts.) %-15s— left-justified, 15-wide columns give the clean side-by-side table without manual tab counting.
Time and Space Complexity
| Aspect | Complexity | Why |
|---|---|---|
| Comparisons | O(n²) | bubble sort’s nested passes |
| Each comparison/swap | O(L) | proportional to string length L |
| Total time | O(n² × L) | |
| Space | O(n × L) | the arrays; sort itself is in-place |
What This Program Teaches
- Strings compare with
strcmp(), never with relational operators - Strings swap with
strcpy()through a temp buffer, never with= - Bounded
scanfwidths as the first line of buffer-overflow defense - Why
strcmpi-style functions break portability, and how to replace them
Related C Programs
- Bubble Sort in C — the same algorithm on integers
- Sort the Characters of a String in C
- Compare Two Strings in C —
strcmpfrom the inside - Selection Sort in C — fewer swaps, same O(n²)
Test yourself: our free C Programming Quiz app for Android has 150+ questions with explanations for every answer.
Recommended Book
The C Programming Language by Kernighan & Ritchie remains the definitive C reference — we’ve solved all of its exercises. Also on Amazon.com.