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