Last active
December 11, 2015 14:28
-
-
Save Aaron1011/4614116 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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]))) { | |
printf("j = %i\n", j); | |
for (; lastchar-'0' <= s1[i]; lastchar++, j++) | |
s2[j] = lastchar;printf("In for loop\n"); | |
state = CHAR; | |
} | |
else { | |
printf("\nError: Mismatched shorthand sequence\n"); | |
return; | |
} | |
} | |
} | |
} | |
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