Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <ctype.h>
- #include <string.h>
- int expand(char *s1, char *s2)
- {
- while (!isdigit(*s1) && !isalpha(*s1))
- {
- *s2 = *s1;
- s1++;
- s2++;
- }
- while(*s1)
- {
- if (!(*s2 = *s1)) // End of line
- return 0;
- char start = *s1;
- if (*(s1 + 1) == '-')
- {
- s1 += 2;
- if (!(*s1)) // End of line
- {
- *s2++ = start;
- *s2++ = '-';
- *s2 = *s1;
- return 0;
- }
- if (start <= *s1 && ((isdigit(*s1) && isdigit(start)) ||
- (isalpha(*s1) && isalpha(start))))
- {
- while (start <= *s1)
- {
- *s2++ = start;
- start++;
- }
- s1++;
- }
- else
- return -1; // Incorrect sequence
- }
- else
- *s2++ = *s1++;
- }
- *s2 = *s1;
- return 0;
- }
- int main(void)
- {
- char s1[256] = "a-z";
- char s2[256];
- if (expand(s1, s2))
- {
- printf("Incorrect sequence!\n");
- return -1;
- }
- printf("%s\n", s1);
- printf("%s\n\n", s2);
- strcpy(s1, "0-9");
- if (expand(s1, s2))
- {
- printf("Incorrect sequence!\n");
- return -1;
- }
- printf("%s\n", s2);
- printf("%s\n\n", s2);
- strcpy(s1, "a-z0-9");
- if (expand(s1, s2))
- {
- printf("Incorrect sequence!\n");
- return -1;
- }
- printf("%s\n", s2);
- printf("%s\n\n", s2);
- strcpy(s1, "a-z0-9-");
- if (expand(s1, s2))
- {
- printf("Incorrect sequence!\n");
- return -1;
- }
- printf("%s\n", s2);
- printf("%s\n\n", s2);
- strcpy(s1, "a-z qwerty 0-9-");
- if (expand(s1, s2))
- {
- printf("Incorrect sequence!\n");
- return -1;
- }
- printf("%s\n", s2);
- printf("%s\n\n", s2);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement