Skip to content

Instantly share code, notes, and snippets.

@Aaron1011
Created January 25, 2013 01:19
Show Gist options
  • Save Aaron1011/4630706 to your computer and use it in GitHub Desktop.
Save Aaron1011/4630706 to your computer and use it in GitHub Desktop.
shorthand.c
#include <stdio.h>
#define MAXCHAR 1000
#define MINCAP 65
#define MAXCAP 90
#define MINDIGIT 48
#define MAXDIGIT 57
#define MINLOWER 97
#define MAXLOWER 122
#define START 0
#define CHAR 1
#define DASH 2
#define LEAD_DASH 3
#define FINISH_STATE 4
void expand(char s1[], char s2[]);
int isalphanum(char c);
int isalpha(char c);
int isnum(char c);
int main() {
int i;
char s1[MAXCHAR];
for(i = 0; i <= MAXCHAR; i++)
s1[i] = 0;
expand("a-z", s1);
printf("%s\n", s1);
}
void expand(char s1[], char s2[]) {
int i, j, state;
char lastchar;
state = START;
for (i = j = 0; s1[i] != '\0'; i++, j++) {
printf("Lastchar = %c\n", lastchar);
if (state == START) {
if (s1[i] == '-')
state = LEAD_DASH;
else
state = CHAR;
lastchar = s2[j] = s1[i];
}
else if (state == LEAD_DASH) {
if (isalphanum(s1[i])) {
state = CHAR;
lastchar = s2[j] = s1[i];
}
else {
printf("\nError: Non-alphanumeric character following hyphen\n");
return;
}
}
else if (state == CHAR) {
if (s1[i] == '-')
state = DASH;
else
lastchar = s2[j] = s1[i];
}
else if (state == DASH) {
if ((isalpha(lastchar) && isalpha(s1[i])) || (isnum(lastchar) && isnum(s1[i]))) {
for (; lastchar-'0' <= s1[i]-'0'; lastchar++, j++) {
char diff = lastchar;
int s1char = s1[i];
printf("New values: %c %d\n", diff ,j);
s2[j] = lastchar;
printf("s2: %s\n", s2);
}
printf("s2: %s\n", s2);
state = CHAR;
}
else {
printf("\nError: Mismatched shorthand sequence\n");
return;
}
}
}
s2[j+1] = '\0';
}
int isalpha(char c) {
return (c-'0' <= MAXCAP && c-0 >= MINCAP) || (c-'0' <= MAXLOWER && c-'0' >= MINLOWER);
}
int isnum(char c) {
return (c-'0' <= MAXDIGIT && c-'0' >= MINDIGIT);
}
int isalphanum(char c) {
return isalpha(c) && isnum(c);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment