/*a program that sorts the strings of an array using a bubble sorts
-use strcmp, strcpy, bubblesorting
-input number of strings: 3
-input string :
zero
one
two
-output string:
one
two
zero*/
#include <stdio.h>
#include <string.h>
#define MAX_LENGTH 100
void bubbleSort(char userArr[][MAX_LENGTH], int size);
int main() {
char userNumbers1[MAX_LENGTH];
char userNumbers2[MAX_LENGTH];
char userNumbers3[MAX_LENGTH];
char temp[3][MAX_LENGTH];
printf("enter three numbers in alphabet: \n");
fgets(userNumbers1, sizeof(userNumbers1), stdin);
fgets(userNumbers2, sizeof(userNumbers2), stdin);
fgets(userNumbers3, sizeof(userNumbers3), stdin);
printf("input: %s\n %s\n %s", userNumbers1, userNumbers2, userNumbers3);
strcpy(temp[0], userNumbers1);
strcpy(temp[1], userNumbers2);
strcpy(temp[2], userNumbers3);
bubbleSort(temp, 3);
printf("sorted: %s %s %s", temp[0], temp[1], temp[2]);
return 0;
}
void bubbleSort(char userArr[][MAX_LENGTH], int size){
char temp[MAX_LENGTH];
for(int i = 0; i < size - 1; i++){
for(int j = 0; j < size - 1 - i; i++)
{
if(strcmp(userArr[j], userArr[j+1]) > 0)
{
strcpy(temp, userArr[j+1]);
strcpy(userArr[j+1], userArr[j]);
strcpy(userArr[j], temp);
}
}
}
}
To embed this project on your website, copy the following code and paste it into your website's HTML: