//=========================================
//bucketstrtok v1 8-8-26???
//=========================================
//
//----------------------------------------
#include <stdio.h>
#include <string.h>
#include <ctype.h>
//----------------------------------------
#define STORE 100000
#define SIZELINE 1000
#define NOLINE 500
#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"
#define ZERO 0
#define ONE 1
#define DELim "',;: ./\t\n"
#define Bucks09AZaz "0123456789ABCDEFGHIJKLMNOPQRSTUVWXZabcdefghijklmnopqrstuvwxyz\0"
#define Maxcharplace 2
//----------------------------------------
//
//----------------------------------------
void bucksort(char str[], const char del[],int charplace , char result[]){
//bucksort(str, del, charplace, result);
//
if (str[0]=='\0') return;
if (del[0]=='\0') return;
if (charplace > Maxcharplace) {
strcpy(result,str);
return;
}
//
// Enough space for concatenated tokens per buckets
const char *digitMap = Bucks09AZaz ;
const int buckno = strlen(Bucks09AZaz);
char buck[buckno][SIZELINE];
char *token;
//---------------------------------------
// Initialize buckets to empty strings
for (int i = ZERO; i <= buckno; i++)
buck[i][0] = '\0';
//---------------------------------------
// Tokenize input string
token = strtok(str, del);
while (token != NULL) {
int val = ZERO;
for (int i = ZERO; i <= buckno; i++){
if (token[charplace] == digitMap[i]){
val = i;
break;
}
}
strcat(buck[val], token);
strcat(buck[val], " ");
token = strtok(NULL, del);
}
//---------------------------------------
// 2D 'buck' array into a 1D 'result'
result[0] = '\0';//""; // Ensure result is empty initially
char tmpbuck[SIZELINE]="";//temp string for bucket
//
if (charplace==1) strcat(result," \n ");
charplace++;
for (int i = ZERO; i <= buckno; i++) {
if (strlen(buck[i]) > 0) {
tmpbuck[0]='\0';
bucksort(buck[i],del,charplace,tmpbuck);
strcat(result, tmpbuck);
buck[i][0]='\0';
}
}
if (charplace==2) strcat(result,",\0");
//---------------------------------------
return;
}
//----------------------------------------
//note in english
//words 4.7 to 5.2 letters
//lines 50 to 75 characters(including spaces)
//----------------------------------------
int main() {
char str[STORE]=""; //TESTSTR ;
char result[STORE] = "";// Ensure sufficient space
char del[10] = DELim ;
int charplace=0;
scanf("%[^\n]", str);// scanf("%s", str);
//gets(str);
printf("%s", str);
bucksort(str, del, charplace, result);
printf("\n \nBucket sort-\n %s", result);
return 0;
//----------------------------------------
}
//---------------------------------------
To embed this project on your website, copy the following code and paste it into your website's HTML: