In C programming, strings are defined as arrays of characters terminated by null (). The character strings are used in building readable and meaningful programs. During programming, a number of operations are required to be performed on strings such as copying a string to a specified location, combing two strings, comparing and sorting etc. In this tutorial post, we’re going to discuss about Sorting Strings Alphabetically in C, with source code and sample output.
The basic working principle of string sorting is comparison of strings. During alphabetical string sorting, a string is compared to other by its character. Moreover, the capital letters are given more priority to small. Before going to the C program source code of string sorting, here is a simple algorithm that points out how string sorting is done.
Algorithm:
- Start
- Declare and define a function for sting comparison
- Enter the number of strings to be sorted
- Input all the strings
- Allocate the memory
- Call the function to sort the strings in alphabetical order
- Print the sorted strings
- Stop
Source Code for Sorting Strings Alphabetically in C:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void arrange(int n, char *x[])// function definition
{
char *temp; // Pointer declaration
int i,str;
for(str = 0; str < n-1; ++str)
{
for(i = str+1; i < n; ++i)
{
if(strcmp(x[str],x[i]) > 0) //comparing the strings
{
temp = x[str]; // compared string being stored in temp
x[str] = x[i];
x[i] = temp;
}
}
}
return;
}
int main()
{
int i , n; // n is the number of strings
char *x[10]; // defining the pointer of array
printf("Enter the number of strings to be sorted: ");
scanf("%d",&n);
printf("Enter the Strings: \n");
for(i = 0; i < n; i++) // to get the strings
{
x[i] = (char*) malloc (12*sizeof(char)); // Memory allocation
printf("String %d: ",i+1);
scanf("%s", x[i]);
}
printf("The sorted strings are: ");
arrange(i,x); // function call with i and x as arguments
for(i = 0; i < n; i++)
{
printf("\nString %d: %s", i+1, x[i]);
}
return 0;
}
This source code includes three header files: stdio.h, string.h and stdlib.h. The stdio.h is used to control standard input and output function such as printf(), scanf(), etc. As the program uses strcmp() function to compare strings, it includes the header file string.h. And, for the purpose of string conversion routines and memory allocation, another header file stdlib.h has been used.
When the C program is executed, it asks for number of strings to be sorted and then the strings by using loop. As the number of strings to be stored may be large, it allocates memory using malloc() function. When all the strings are entered, user defied function arrange() is called for sorting strings alphabetically.
The user defined function utilizes the strcmp() function to compare strings. The compared strings are stored in temp which has been declared as pointer of character. Depending upon the characters of string, they are swapped with temp and arranged. Reordered string is returned to main() function and displayed in console output.
I hope this tutorial on sorting strings alphabetically in C was helpful to you. The source code is short and simple to understand; you can go through the description provided below the source code to understand how the program operates. Source code is compiled on Code::Blocks IDE. If you have any suggestions or questions regarding this post or source code for strings sorting, mention/discuss them in the comments section.
You can find more C Tutorials here.
Thanx mate u’ve helped me a lot I was almost stuck with my assingment