Introduction:

This C program is designed to sort the characters of a given string in alphabetical order. The program reads a string from the user, processes it using a sorting algorithm, and outputs the sorted string. This functionality is useful in various applications such as data organization, text formatting, and analysis, enabling users to easily manage and view their data in a structured manner.

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

void sortString(char *str) {
    int length = strlen(str);
    for (int i = 0; i < length - 1; i++) {
        for (int j = i + 1; j < length; j++) {
            if (str[i] > str[j]) {
                char temp = str[i];
                str[i] = str[j];
                str[j] = temp;
            }
        }
    }
}

int main() {
    char str[100];
    printf("Enter a string: ");
    fgets(str, sizeof(str), stdin);
    str[strcspn(str, "\n")] = 0; // Remove newline character

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

Include Header Files:

  • #include <stdio.h>: This header file is included for standard input/output functions like printf and fgets.
  • #include <string.h>: This header file is included for string manipulation functions such as strlen.

Function to Sort the String:

  • void sortString(char *str): This function takes a string as input.

Declare Variables:

  • int length = strlen(str);: Calculates the length of the input string.
  • char temp;: A temporary variable used for swapping characters during sorting.

Bubble Sort Algorithm:

  • The sorting is done using the bubble sort algorithm, which is a simple comparison-based algorithm.
  • Outer Loop: for (int i = 0; i < length - 1; i++): This loop runs from the start of the string to the second-to-last character.
  • Inner Loop: for (int j = 0; j < length - i - 1; j++): This loop compares adjacent characters. It runs fewer times as the largest unsorted elements are moved to the end with each pass.
  • Comparison and Swapping: if (str[j] > str[j + 1]): If the current character is greater than the next one, the two characters are swapped using the temporary variable temp.

Main Function:

  • char str[100];: Declares an array to hold the user-input string.
  • printf("Enter a string: ");: Prompts the user to enter a string.
  • fgets(str, sizeof(str), stdin);: Reads a line of text from standard input, allowing spaces.
  • str[strcspn(str, "\n")] = 0;: Removes the newline character that fgets adds.
  • sortString(str);: Calls the function to sort the string alphabetically.
  • printf("Sorted string: %s\n", str);: Outputs the sorted string.

Return Statement:

  • return 0;: Indicates successful completion of the program.

Input/Output Block:

Input:

  • The user is prompted to enter a string containing characters.

Example Input:

Enter a string: Hello World

Output:

  • The program outputs the string with its characters sorted alphabetically.

Example Output:

Sorted string:  HWdellloo

Conclusion:

The string sorting program effectively organizes the characters of a string in alphabetical order, demonstrating fundamental concepts in sorting algorithms and string manipulation in C. By employing a simple bubble sort algorithm, the program illustrates how to rearrange data efficiently. This capability can be applied in various contexts, such as data analysis, user input processing, and improving text readability. Overall, the program serves as a practical example of algorithm implementation and provides a valuable tool for developers and data analysts.

Leave a Reply

Your email address will not be published. Required fields are marked *

Verified by MonsterInsights