C Program to sort a set of strings in ascending alphabetical order

In this C Programming example, we will implement the program to sort a set of strings in ascending alphabetical order and print the output on the console.

1. Sort set of strings – Selection Sort

To sort a set of strings in ascending alphabetical order, we will first iterate over the characters and then compare two characters at a time at index 0 positions.

If the characters are not found in the correct order, we will swap the characters using a temporary variable and repeat the process until we reach a null character(or no more characters are left for comparison).

This process of sorting is also known as Selection sort. There are different sorting techniques that can be used like Bubble sort, Insertion sort etc. Here is a list of some other sorting algorithms.

Example: 

Input: 
hello
codingeek
world
programming

Sorted Output: 
codingeek
hello
programming
world

Helpful topics to understand this program better are-


2. C Program to sort a set of strings in ascending alphabetical order

Let’s discuss the execution(kind of pseudocode) for the program to sort a set of strings in ascending alphabetical order in C.

  1. Initially, the program will prompt the user to enter the number of sets of strings.
  2. Then a function sortStringsInAplhabeticalAsecOrder() is invoked and using for loop and temporary variable to perform the calculations.
  3. The obtained results are then printed on the console.

Note: The strcpy() function copies the string pointed by the source (including the null character) to the destination. The function also returns the copied string.

Let us now implement the above execution of the program to sort a set of strings in ascending alphabetical order in C.

#include <stdio.h>
#include <string.h>

char str[25][25];
int count;
void sortStringsInAplhabeticalOrder() {

  for (int i = 0; i <= count; i++) {
    for (int j = i + 1; j <= count; j++) {
      if (strcmp(str[i], str[j]) > 0) {
        char temp[25];
        strcpy(temp, str[i]);
        strcpy(str[i], str[j]);
        strcpy(str[j], temp);
      }
    }
  }
}
int main() {
  puts("Enter the number of strings: ");
  scanf("%d", &count);
  puts("Enter Strings(followed by enter): ");

  for (int i = 0; i <= count; i++) {
    gets(str[i]);
  }

  sortStringsInAplhabeticalOrder();

  printf("Sorted Strings:");

  for (int i = 0; i <= count; i++) {
    puts(str[i]);
  }
  return 0;
}

Note: In the above program the characters must be entered under the declared range char str[25][25], temp[25]. To sort the characters in descending order we just have to flip one condition and everything else remains the same.

Output
Enter the number of strings:
5

Enter Strings(followed by enter):
codingeek
python
data structures
algorithms
programming

Sorted Strings:
algorithms
codingeek
data structures
programming
python

3. C Program to sort string in Descending order

To sort the string in descending order we just have to reverse the condition and everything remains the same. So the method sortStringsInAplhabeticalOrder in the above program becomes –

void sortStringsInAplhabeticalOrder() {

  for (int i = 0; i <= count; i++) {
    for (int j = i + 1; j <= count; j++) {
      if (strcmp(str[i], str[j]) > 0) {
        char temp[25];
        strcpy(temp, str[i]);
        strcpy(str[i], str[j]);
        strcpy(str[j], temp);
      }
    }
  }
}

4. Conclusion

In this C Programming example, we have discussed how to sort characters in a string in ascending alphabetical order in C.


Helpful Links

Please follow C Programming tutorials or the menu in the sidebar for the complete tutorial series.

Also for the example C programs please refer to C Programming Examples.

All examples are hosted on Github.


Recommended Books


An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding

Do not forget to share and Subscribe.

Happy coding!! ?

Recommended -

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
Index