//=========================================
//buck sort 5~4-7-26a 1v tested works
//=========================================
//----------------------------------------
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define TESTSTR " Now is the winter of our discontent Made glorious \n summer by this sun of York; And all the clouds that \n lour'd upon our house"
//
//----------------------------------------
void labelformat(int j, char result[]){
//labelformat(j, result);
char label[30];
sprintf(label, "~ :%c:", ' ' + j);
strcat(result, label);
strcat(result, "\n");
}
//----------------------------------------
void bucksort(char str[], char del[], char result[]){
//bucksort(str, del, result);
char *token;
char buck[100][1000];
// Enough space for concatenated
// tokens per bucket
//---------------------------------------
// Initialize buckets to empty strings
char cmin = ' ', cmax = '~';
for (int i = cmin-cmin; i < cmax-cmin; i++)buck[i][0] = '\0';
//---------------------------------------
// Tokenize input string
token = strtok(str, del);
while (token != NULL) {
// Concatenate token and space to the correct bucket
// Convert first character for indexing
char c = token[0];
if (c >=cmin && c <= cmax) {
//cover ascii ' '<'~'
strcat(buck[c - cmin], token);
strcat(buck[c - cmin], " ");
}
else if (c < cmin){
//all lower char values
strcat(buck[cmin-cmin], token);
strcat(buck[cmin-cmin], " ");
}
else if (c > cmax){
//all upper char values
strcat(buck[cmax-cmin], token);
strcat(buck[cmax-cmin], " ");
}
token = strtok(NULL, del);
}
//---------------------------------------
// 2D 'buck' array into a 1D 'result'
result[0] = '\0'; // Ensure result
//is empty initially
for (int j = cmin-cmin; j < cmax-cmin; j++) {
if (strlen(buck[j]) > 0) {
strcat(result, buck[j]);
labelformat(j, result);
}
}
}
//----------------------------------------
int main() {
char str[] = TESTSTR ;
char result[1000] = ""; // Ensure sufficient
// space
char del[10] = "'#; ./'";
printf("%s", str);
bucksort(str, del, result);
printf("%s", result);
return 0;
//----------------------------------------
}
//---------------------------------------
//char c = toupper(token[0]);
//if (c >= 'A' && c <= 'Z') {
//int index = c - 'A';
//---------------------------------------
To embed this project on your website, copy the following code and paste it into your website's HTML: