Expands shorthand notations like a-z into the equivalent complete list abc...xyz - C String

C examples for String:String Function

Description

Expands shorthand notations like a-z into the equivalent complete list abc...xyz

Demo Code

#include <stdio.h>

#define MAXLINE 1000//ww w.j  a v a 2  s.c  o  m

void expand(char s1[], char s2[]);

int main(void){
    char s1[MAXLINE] = "a-z 1-9 a-9 A-Z";
    char s2[MAXLINE];

    expand(s1, s2);
    printf("\nThe expanded result is:\n%s\n", s2);

    return 0;
}
void expand(char s1[], char s2[]){
    int i, j, k;
    int c;

    for (i = 0, j = 0; s1[i] != '\0'; ++i) {
        if (s1[i+1] == '-' && (c = s1[i+2]) != '\0' &&
            (('a' <= s1[i] && s1[i] <= 'z' && s1[i] <= c && c <= 'z') ||
            ('A' <= s1[i] && s1[i] <= 'Z' && s1[i] <= c && c <= 'Z') ||
            ('0' <= s1[i] && s1[i] <= '9' && s1[i] <= c && c <= '9'))) {
                k = 0;
                while (k <= c - s1[i])
                    s2[j++] = s1[i] + k++;
                i += 2;
        } else {
            s2[j++] = s1[i];
        }
    }
    s2[j] = '\0';
}

Result


Related Tutorials