Files
exc/c/stringsort.c
2026-06-30 07:01:23 +02:00

73 lines
1.6 KiB
C

/*
idee, een programma dat een lijst van strings krijgt, en ze dan sorteert op
alphabetische volgorde, idealiter ook met hele nummbers if relevant (dus niet
alleen individuele digits)
split into strings and numbers
okay dit werkt nu omzetten naar strings
*/
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define WORDS 16
#define WORDLENGTH 64
/* return 1 if word1 < word 2*/
int compareword(char word1[], char word2[]) {
for (int i = 0; i < WORDLENGTH; i++) {
if (word1[i] == word2[i]) {
//it should continue
}
else if (word1[i] < word2[i]) {
printf("%s is larger than %s\n", word1, word2);
return 1;
}
else {
return 0;
}
}
return 0;
}
/* eerst gewone bubblesort weer doen */
int mybubblesort(char inputarr[WORDS][WORDLENGTH]) {
for (int i = 0; i < WORDS - 1; i++) { // voor elk woord
for (int j = 0; j < WORDS - 1; j++) { // ga langs elk woord
if (compareword(inputarr[j], inputarr[j + 1])) { // als huidig nummer groter is
// swap hem dan
char temp[64];
strcpy(temp, inputarr[j+1]);
strcpy(inputarr[j+1], inputarr[j]);
strcpy(inputarr[j], temp);
}
}
}
for (int i = 0; i < WORDS - 1; i++) {
if (inputarr[i][0] > 0) {
printf("num:%i, %s\n", i, inputarr[i]);
}
//puts(inputarr[i]);
}
return 0;
}
int main() {
char mytest[16][64] = {"pear", "watermelon", "aap", "apple", "banana", "cantaloupe", "Elephant", "Zebra"};
srand(time(NULL));
mybubblesort(mytest);
return 0;
}