Write a function expand(s1,s2) that expands shorthand notations like a-z in the string s1 into the equivalent complete list abc...xyz in s2. Allow for letters of either case and digits, and be prepared to handle cases like a-b-c and a-z0-9 and -a-z and a--z. Arrange that a leading or trailing - is taken literally.
SAMPLE INPUT:
a-z
A-A
0-9
c--z
-a-c
a-cf
a-c-f
SAMPLE OUTPUT:
abcdefghijklmnopqrstuvwxyz
A
0123456789
c--z
-abc
abcf
abcdef
#include
#include
#include
const int N = 10001;
void expand(char [], char []);
int main()
{
// freopen("0.in", "r", stdin);
// freopen("0.out", "w", stdout);
char s1[N], s2[N * 30];
while(scanf("%s", s1) != EOF) {
expand(s1, s2);
printf("%sn", s2);
}
return 0;
}
void expand(char s1[],char s2[]){
int i,j;
i=j=0;
int length;
while(s1[i]!=' '){
switch (s1[i]) {
case '-':
if(i==0||s1[i+1]-s1[i-1]<0){
i++;
s2[j++]='-';
break;
}
if(islower(s1[i-1]) && islower(s1[i+1]) || isupper(s1[i-1]) && isupper(s1[i+1]) || isdigit(s1[i-1]) && isdigit(s1[i+1])){
length =s1[i+1]-s1[i-1];
for(int z=0;z<=length;z++){
s2[j-1+z]=s1[i-1]+z;
}
i=i+2;
j=j+length;
}
else{
i++;
s2[j++]='-';
}
break;
default:
s2[j++]=s1[i++];
break;
}
}
s2[j]=' ';
}
abcdefghijklmnopqrstuvwxyz A 0123456789 c--z -abc abcf abcdef
#include#include #include const int N = 10001; void expand(char [], char []); int main() { // freopen("0.in", "r", stdin); // freopen("0.out", "w", stdout); char s1[N], s2[N * 30]; while(scanf("%s", s1) != EOF) { expand(s1, s2); printf("%sn", s2); } return 0; } void expand(char s1[],char s2[]){ int i,j; i=j=0; int length; while(s1[i]!=' '){ switch (s1[i]) { case '-': if(i==0||s1[i+1]-s1[i-1]<0){ i++; s2[j++]='-'; break; } if(islower(s1[i-1]) && islower(s1[i+1]) || isupper(s1[i-1]) && isupper(s1[i+1]) || isdigit(s1[i-1]) && isdigit(s1[i+1])){ length =s1[i+1]-s1[i-1]; for(int z=0;z<=length;z++){ s2[j-1+z]=s1[i-1]+z; } i=i+2; j=j+length; } else{ i++; s2[j++]='-'; } break; default: s2[j++]=s1[i++]; break; } } s2[j]=' '; }



