Write a C program that will read a word and rewrite it in alphabetical order

7/26/2022

C program to sort a string ,Sort a string in Alphabetical order in c language

Go Back

C Program to Sort a String in Alphabetical Order (with C++ Example)

Introduction

Sorting a string alphabetically is a common programming task in C and C++. In this tutorial, we will write a C program to arrange the characters of a string in alphabetical order. Additionally, we will also see how to perform the same task in C++ using the built-in sort function.

Sorting a string is useful in applications like dictionary sorting, word manipulation, and text processing.


C program to sort a string ,Sort a string in Alphabetical order in c language

C Program to Sort a String in Alphabetical Order

The following C program reads a string from the user and sorts its characters in ascending order (A-Z or a-z).

Code: Sorting a String in C

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

void main() {
    char str[100], temp;
    int i, j;

    printf("Enter a string: ");
    scanf("%[^
]", str);

    int length = strlen(str);

    // Sorting characters using Bubble Sort
    for(i = 0; i < length - 1; i++) {
        for(j = i + 1; j < length; j++) {
            if(str[i] > str[j]) {
                // Swap characters
                temp = str[i];
                str[i] = str[j];
                str[j] = temp;
            }
        }
    }

    printf("Sorted string: %s\n", str);
}

Explanation

  1. The program takes a string as input from the user.
  2. It sorts the string using Bubble Sort (a simple comparison-based sorting technique).
  3. Finally, it prints the sorted string.

Example Output

Enter a string: HELLO
Sorted string: EHLLO

C++ Program to Sort a String Alphabetically

The same task can be performed in C++ using the sort() function from the <algorithm> library.

Code: Sorting a String in C++

#include <iostream>
#include <algorithm>
#include <string>

using namespace std;

int main() {
    string str;
    cout << "Enter a string: ";
    cin >> str;

    // Sorting using the built-in sort() function
    sort(str.begin(), str.end());

    cout << "Sorted string: " << str << endl;
    return 0;
}

Explanation

  1. We use C++'s built-in sort() function, which is more efficient than manually implementing sorting.
  2. The function rearranges the characters in ascending order.

Example Output

Enter a string: HELLO
Sorted string: EHLLO

Key Differences Between C and C++ Approach

Feature C Approach C++ Approach
Sorting Method Bubble Sort (Manual Sorting) Built-in sort() function
Code Complexity Higher (Nested Loops) Lower (One Line Sort)
Efficiency O(n²) (Slow for large strings) O(n log n) (Faster Sorting)

Use Cases of Sorting a String in C & C++

Sorting strings alphabetically is useful in: ✔ Lexicographic sorting – Used in dictionaries and word processing
Anagram detection – Checking if two words have the same letters
Text analysis – Preprocessing text data for machine learning
Generating permutations – Useful in cryptography and data processing


Conclusion

In this tutorial, we learned how to sort a string alphabetically in C and C++. The C program uses Bubble Sort, while the C++ program takes advantage of the sort() function from the algorithm library.

If you’re working with large datasets, C++ is recommended due to its optimized sorting functions. Mastering string sorting techniques is crucial for programming interviews and competitive coding! 🚀


Related Topics & Further Learning

🔹 C Program to Reverse a String
🔹 C++ Program to Check Anagram Strings
🔹 Sorting Algorithms in C++

Let me know if you need further improvements! 😊 🚀