C Online Compiler
Example: Sort String Descending (Bubble Sort) in C
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Sort String Descending (Bubble Sort) #include <stdio.h> #include <string.h> // Required for strlen() int main() { char str[] = "programming"; int length = strlen(str); int i, j; char temp; printf("Original string: %s\n", str); // Step 1: Outer loop for passes through the string for (i = 0; i < length - 1; i++) { // Step 2: Inner loop for comparisons and swaps for (j = 0; j < length - 1 - i; j++) { // Step 3: Compare adjacent characters // For descending order, if current is smaller than next, swap if (str[j] < str[j+1]) { // Step 4: Swap characters temp = str[j]; str[j] = str[j+1]; str[j+1] = temp; } } } // Step 5: Print the sorted string printf("Sorted string (descending): %s\n", str); return 0; }
Output
Clear
ADVERTISEMENTS