编写函数expand(s1,s2), 将字符串s1中类似于a-z一类的速记符号在字符串s2中扩展为等价的完整列表abc……xyz。该函数可以处理大小写字母和数字,并可以处理a-b-c、a-z0-9与a-z等类似的情况。作为前导和尾随的字符原样复制
#include<stdio.h> #include<ctype.h> #include<string.h> int judge(char a, char b) //判断'-'两端的字符是否符合速记符号扩展的要求 { if(isdigit(a) && isdigit(b)) { if(a < b) return 1; } if(isalpha(a) && isalpha(b)) { if(isupper(a) && isupper(b)) return 1; if(islower(a) && islower(b)) return 1; } return 0; } void expand(char *s, char *t) { int i, j; char c; i = j = 0; while((c = s[i++]) != '\0') { if((s[i] == '-') && judge(c,s[i + 1])) { i++; while(c < s[i]) t[j++] = c++; } else { t[j++] = c; } } s[j] = '\0'; } int main() { char a[1000],b[1000]; gets(a); expand(a,b); printf("%s\n",b); return 0; }