I'm trying to do the complete exercise of KnR i.e. deal with things like:

a-z0-9

I coded two shellsorts: one for integers and the other for chars to deal with this problem.

**datastructures.h**

Code:

int binsearch(int, int*, int);
void swap(int*, int*);
void Shellsort(int*, int); /* please note the uppercase - it's the integer shellsort */

**shellsort.c **

Code:

#include "../Headers/datastructures.h"
#include <stdio.h>
void Shellsort(int* a, int n)
{
int gap, i, j, temp;
for(gap = n/2; gap > 0; gap /= 2)
for(i = gap; i < n; i++)
for(j = i - gap; j >= 0 && a[j] > a[j + gap]; j -= gap)
{
temp = a[j];
a[j] = a[j + gap];
a[j + gap] = temp;
}
}

**expand.c **

Code:

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include "Headers/datastructures.h"
#include "DataStructures/shellsort.c"
#ifndef ALPHA
#define ALPHA 26
#endif
#ifndef MAXSIZE
#define MAXSIZE 20
#endif
static int nLetters, nDigits;
static int digits[MAXSIZE];
static char letters[MAXSIZE];
void lettersdigits(char*);
void expand(char*);
void shellsort(char*, int);
void shellsort(char* a, int n)
{
int gap, i, j, temp;
for(gap = nLetters/2; gap > 0; gap /= 2)
for(i = gap; i < nLetters; i++)
for(j = i - gap; j >= 0 && a[j] > a[j + gap]; j -= gap)
{
temp = a[j];
a[j] = a[j + gap];
a[j + gap] = temp;
}
}
void lettersdigits(char* s)
{
int i, j, z;
for(i = 0, j = 0, z = 0; i < strlen(s); i++)
{
if(isalpha( (unsigned char) s[i]) )
{
letters[j++] = s[i];
nLetters++;
}
if(isdigit(s[i]))
{
digits[z++] = s[i];
nDigits++;
}
}
letters[j] = '\0';
digits[z] = '\0';
}
void expand(char* s)
{
int c;
lettersdigits(s);
shellsort(letters, nLetters);
Shellsort(digits, nDigits);
printf("The expanded string is ");
for(c = letters[0]; c < s[nLetters]; c++)
putchar(c);
for(c = digits[0]; c < s[nDigits]; c++)
putchar(c);
putchar('\n');
}
int main(void)
{
char *s = malloc(MAXSIZE * sizeof(char));
do {
printf("Enter letters and digits separated by hyphens: ");
fgets(s, MAXSIZE, stdin);
} while(strlen(s) > MAXSIZE);
printf("You entered: %s\n", s);
expand(s);
free(s);
return 0;
}

**output: **

Code:

./expand
Enter letters and digits separated by hyphens: a-z0-9
You entered: a-z0-9
The expanded string is abcdefghijklmnopqrstuvwxy0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxy

Code:

./expand
Enter letters and digits separated by hyphens: a-z
You entered: a-z
The expanded string is abcdefghijklmnopqrstuvwxy
!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`

**edit: **

I fixed the two for's:

Code:

for(c = letters[0]; c < letters[nLetters]; c++)
putchar(c);
for(c = digits[0]; c < digits[nDigits]; c++)
putchar(c);

but now I get:

Code:

./expand
Enter letters and digits separated by hyphens: a-z0-9
You entered: a-z0-9
The expanded string is