Sorting strings is a fundamental concept in C programming. It is widely used in applications like managing lists of names, words, or other textual data. Sorting helps make data organized and readable, and learning different methods teaches important programming skills such as string manipulation, array handling, pointers, and algorithm design.
In this guide, we will cover multiple approaches to sort strings alphabetically in C, including Bubble Sort, Selection Sort, pointer-based swaps, standard library qsort()
, and advanced case-insensitive sorting. Each method is explained step by step, with full code examples. By the end, you will have a complete understanding of string sorting in C.
Understanding the Problem
The goal is to take a list of strings and arrange them in ascending alphabetical order. For example:
- Input:
"Scorpion", "Sub-Zero", "Raiden"
- Output:
"Raiden", "Scorpion", "Sub-Zero"
Sorting can be case-sensitive (distinguishing uppercase from lowercase) or case-insensitive (ignoring case differences). We will explore both.
In C, strings are arrays of characters ending with a null character \0
. Comparing two strings is done using either strcmp()
or a custom function that compares each character. For case-insensitive sorting, functions like tolower()
or strcasecmp()
are useful.
Method 1: Bubble Sort Using strcmp()
Bubble Sort repeatedly compares adjacent strings and swaps them if they are out of order. It is simple and good for beginners.
#include <stdio.h>
#include <string.h>
int main() {
char str[5][100], temp[100];
int i, j, n = 5;
printf("Enter 5 strings:\n");
for (i = 0; i < n; i++) {
fgets(str[i], sizeof(str[i]), stdin);
str[i][strcspn(str[i], "\n")] = '\0';
}
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (strcmp(str[j], str[j + 1]) > 0) {
strcpy(temp, str[j]);
strcpy(str[j], str[j + 1]);
strcpy(str[j + 1], temp);
}
}
}
printf("\nSorted strings:\n");
for (i = 0; i < n; i++) {
printf("%s\n", str[i]);
}
return 0;
}
Bubble Sort is easy to understand but has O(n²) complexity, making it inefficient for large arrays.
Method 2: Selection Sort
Selection Sort finds the smallest string and moves it to the beginning, repeating for the rest of the array.
#include <stdio.h>
#include <string.h>
int main() {
char str[5][100], temp[100];
int i, j, min, n = 5;
printf("Enter 5 strings:\n");
for (i = 0; i < n; i++) {
fgets(str[i], sizeof(str[i]), stdin);
str[i][strcspn(str[i], "\n")] = '\0';
}
for (i = 0; i < n - 1; i++) {
min = i;
for (j = i + 1; j < n; j++) {
if (strcmp(str[j], str[min]) < 0) {
min = j;
}
}
if (min != i) {
strcpy(temp, str[i]);
strcpy(str[i], str[min]);
strcpy(str[min], temp);
}
}
printf("\nSorted strings:\n");
for (i = 0; i < n; i++) {
printf("%s\n", str[i]);
}
return 0;
}
Selection Sort is more predictable than Bubble Sort but still O(n²).
Method 3: Using Pointers for Efficient Swaps
Instead of copying entire strings, we can swap pointers, which is more efficient for large strings.
#include <stdio.h>
#include <string.h>
int main() {
char s0[100], s1[100], s2[100], s3[100], s4[100];
char *str[5] = {s0, s1, s2, s3, s4};
char *temp;
int i, j, n = 5;
printf("Enter 5 strings:\n");
for (i = 0; i < n; i++) {
fgets(str[i], 100, stdin);
str[i][strcspn(str[i], "\n")] = '\0';
}
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (strcmp(str[j], str[j + 1]) > 0) {
temp = str[j];
str[j] = str[j + 1];
str[j + 1] = temp;
}
}
}
printf("\nSorted strings:\n");
for (i = 0; i < n; i++) {
printf("%s\n", str[i]);
}
return 0;
}
Pointer swapping avoids copying full strings repeatedly, improving efficiency for large datasets.
Method 4: Using qsort()
The C standard library provides qsort()
which implements Quick Sort and is highly efficient (O(n log n)).
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int compare(const void *a, const void *b) {
return strcmp(*(const char **)a, *(const char **)b);
}
int main() {
char s0[100], s1[100], s2[100], s3[100], s4[100];
char *str[5] = {s0, s1, s2, s3, s4};
int i, n = 5;
printf("Enter 5 strings:\n");
for (i = 0; i < n; i++) {
fgets(str[i], 100, stdin);
str[i][strcspn(str[i], "\n")] = '\0';
}
qsort(str, n, sizeof(char *), compare);
printf("\nSorted strings:\n");
for (i = 0; i < n; i++) {
printf("%s\n", str[i]);
}
return 0;
}
qsort()
is suitable for large arrays and is highly recommended for real-world applications.
Method 5: Case-Insensitive Sorting
For real-world datasets, sorting is often case-insensitive. We can implement this by converting characters to lowercase during comparison:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int compareIgnoreCase(const void *a, const void *b) {
char *str1 = *(char **)a;
char *str2 = *(char **)b;
int i = 0;
while (str1[i] && str2[i]) {
char c1 = tolower((unsigned char)str1[i]);
char c2 = tolower((unsigned char)str2[i]);
if (c1 != c2) return c1 - c2;
i++;
}
return tolower((unsigned char)str1[i]) - tolower((unsigned char)str2[i]);
}
int main() {
char s0[100], s1[100], s2[100], s3[100], s4[100];
char *str[5] = {s0, s1, s2, s3, s4};
int i, n = 5;
printf("Enter 5 strings:\n");
for (i = 0; i < n; i++) {
fgets(str[i], 100, stdin);
str[i][strcspn(str[i], "\n")] = '\0';
}
qsort(str, n, sizeof(char *), compareIgnoreCase);
printf("\nStrings in alphabetical order (case-insensitive):\n");
for (i = 0; i < n; i++) {
printf("%s\n", str[i]);
}
return 0;
}
This approach ensures that "Apple"
and "apple"
are treated the same during sorting.
FAQs
Q1: Can I sort strings without strcmp()
?
Yes, you can compare characters manually using their ASCII values, but strcmp()
is safer and less error-prone.
Q2: Can I sort dynamically allocated strings?
Yes. Allocate an array of pointers with malloc()
and sort them using the same qsort()
methods.
Q3: How do I ignore spaces while sorting?
You can preprocess strings to remove spaces or write a custom comparison function that skips them.
Q4: Which sorting method is best for large datasets?qsort()
is highly efficient and recommended for large arrays due to its O(n log n) time complexity.
Q5: Is case-insensitive sorting always better?
It depends on your application. For user-facing text, case-insensitive sorting is usually preferred. For technical data where case matters, use case-sensitive sorting.
Conclusion
Sorting strings in C can be done in many ways, from simple Bubble Sort to advanced qsort()
with case-insensitive comparisons. Beginners can start with loops or Selection Sort to understand the logic. For real-world applications, qsort()
with pointers and case-insensitive comparison is the most efficient and practical.
By mastering these techniques, you gain strong skills in string manipulation, arrays, pointers, and algorithm design—essential for both programming practice and interviews.
References & Additional Resources
A curated collection of textbooks, tutorials, and documentation for learning string sorting and related functions in C.
- Kernighan, Brian W., and Dennis M. Ritchie. The C Programming Language, 2nd Edition, Prentice Hall, 1988 – The classic reference covering strings, arrays, pointers, and essential C programming concepts.
- GeeksforGeeks: Sorting Strings in C – Explains different approaches to sort strings alphabetically using C, including examples with
strcmp()
and arrays of strings. - Tutorialspoint: C Strings – Overview of string handling, declaration, initialization, and basic operations in C.
- Cprogramming.com: Strings in C – Tutorial on string manipulation, sorting, and comparison techniques in C.
- ISO C Standard Library Reference for
qsort
– Documentation for the standard libraryqsort()
function used for sorting arrays, including arrays of strings. - POSIX
strcasecmp()
Reference – Reference for the case-insensitive string comparison function in C.