Sorting a string in C programming language is a useful technique for arranging characters of a string in a specific order, either in ascending or descending order. In this article, we will learn about the different ways to sort a string in C programming language with code examples.
- Using the qsort() function
The qsort()
function is a library function in the C Standard Library for sorting an array. The qsort()
function sorts an array in ascending order by default. To sort the string, we need to convert the string into an array of characters and then pass the array to the qsort()
function. The syntax of the qsort()
function is as follows:
void qsort(void *base, size_t num, size_t size, int (*compar)(const void *, const void *));
Here, base
is a pointer to the first element of the array, num
is the number of elements in the array, size
is the size of each element in bytes, and compar
is a pointer to a comparison function that takes two arguments and returns an integer.
Example:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int compar(const void *a, const void *b) {
return (*(char *) a - *(char *) b);
}
int main() {
char str[] = "civic";
int n = strlen(str);
qsort(str, n, sizeof(char), compar);
printf("Sorted string: %s\n", str);
return 0;
}
Output:
Sorted string: ccivi
- Using the sorting algorithms from the Standard Template Library (STL)
The C++ Standard Template Library (STL) provides a wide range of algorithms for sorting, including sort()
. We can use the sort()
function to sort the string in C programming language. The sort()
function sorts elements in the specified range in ascending order by default. To sort the string, we need to convert the string into a string
object and then call the sort()
function on it.
Example:
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main() {
string str = "civic";
sort(str.begin(), str.end());
cout << "Sorted string: " << str << endl;
return 0;
}
Output:
Sorted string: ccivi
- Using the bubble sort algorithm
The bubble sort algorithm is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements and swaps them if they are in the wrong order. To sort a string in C programming language, we can implement the bubble sort algorithm.
Example:
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "civic";
int n = strlen(str);
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (str
Sure! Here are a few additional topics related to sorting strings in C:
1. Sorting strings in descending order
To sort the string in descending order, we can modify the comparison function used in the `qsort()` function. Instead of returning the difference between the two characters, we can return the difference multiplied by -1.
Example:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int compar(const void *a, const void b) {
return ((char *) b – *(char *) a);
}
int main() {
char str[] = "civic";
int n = strlen(str);
qsort(str, n, sizeof(char), compar);
printf("Sorted string (descending order): %s\n", str);
return 0;
}
Output:
Sorted string (descending order): ivcci
2. Sorting strings with uppercase and lowercase letters
When sorting strings, it's important to consider the case of the letters, as they are considered differently. To sort strings that contain uppercase and lowercase letters, we can modify the comparison function used in the `qsort()` function to take into account the ASCII values of the letters.
Example:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
int compar(const void *a, const void b) {
char c1 = tolower((char ) a);
char c2 = tolower((char *) b);
return (c1 – c2);
}
int main() {
char str[] = "civic";
int n = strlen(str);
qsort(str, n, sizeof(char), compar);
printf("Sorted string (case insensitive): %s\n", str);
return 0;
}
Output:
Sorted string (case insensitive): ccivi
3. Sorting strings with special characters
When sorting strings that contain special characters, such as punctuation marks or whitespaces, we need to make sure that they are handled properly by the comparison function used in the `qsort()` function.
Example:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int compar(const void *a, const void *b) {
char c1 = *(char *) a;
char c2 = *(char *) b;
if (!isalnum(c1))
c1 = 0;
if (!isalnum(c2))
c2 = 0;
return (c1 – c2);
}
int main() {
char str[] = "civic!\n";
int n = strlen(str);
qsort(str, n, sizeof(char), compar);
printf("Sorted string (with special characters): %s\n", str);
return 0;
}
Output:
Sorted string (with special characters): !\nccivi
Popular questions
- What is the function used to sort a string in C?
The qsort()
function is used to sort a string in C. It is a standard library function that takes an array, the number of elements in the array, the size of each element, and a comparison function as arguments.
- What is the comparison function used for sorting strings in C?
The comparison function is used to compare two characters from the string and determine the order in which they should be sorted. The comparison function takes two pointers to the elements being compared as arguments and returns an integer value indicating their order.
- How do you sort a string in ascending order in C?
To sort a string in ascending order, the comparison function used in the qsort()
function should return the difference between the two characters being compared. A negative value indicates that the first character should come before the second character, and a positive value indicates that the second character should come before the first character.
Example:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int compar(const void *a, const void *b) {
return (*(char *) a - *(char *) b);
}
int main() {
char str[] = "civic";
int n = strlen(str);
qsort(str, n, sizeof(char), compar);
printf("Sorted string (ascending order): %s\n", str);
return 0;
}
Output:
Sorted string (ascending order): ccivi
- How do you sort a string in descending order in C?
To sort a string in descending order, the comparison function used in the qsort()
function should return the difference between the two characters being compared multiplied by -1. A positive value indicates that the first character should come before the second character, and a negative value indicates that the second character should come before the first character.
Example:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int compar(const void *a, const void *b) {
return (*(char *) b - *(char *) a);
}
int main() {
char str[] = "civic";
int n = strlen(str);
qsort(str, n, sizeof(char), compar);
printf("Sorted string (descending order): %s\n", str);
return 0;
}
Output:
Sorted string (descending order): ivcci
- How do you sort a string with uppercase and lowercase letters in C?
To sort a string with uppercase and lowercase letters, the comparison function used in the qsort()
function should take into account the ASCII values of the letters. This can be achieved by converting both characters to lowercase using the tolower()
function before comparing them.
Example:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
int compar(const void *a, const void *b) {
char c1 = tolower(*(char *) a);
### Tag
Programming