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