C程序设计语言习题(33)

编写函数expand(s1,s2), 将字符串s1中类似于a-z一类的速记符号在字符串s2中扩展为等价的完整列表abc……xyz。该函数可以处理大小写字母和数字,并可以处理a-b-c、a-z0-9与a-z等类似的情况。作为前导和尾随的字符原样复制

 1 #include<stdio.h>
 2 #include<ctype.h>
 3 #include<string.h>
 4 
 5 int judge(char a, char b)   //判断'-'两端的字符是否符合速记符号扩展的要求
 6 {
 7     if(isdigit(a) && isdigit(b))
 8     {
 9         if(a < b)  return 1;
10     }
11     if(isalpha(a) && isalpha(b))
12     {
13         if(isupper(a) && isupper(b))  return 1;
14         if(islower(a) && islower(b))  return 1;
15     }
16     return 0;
17 }
18 
19 void expand(char *s, char *t)
20 {
21     int i, j;
22     char c;
23     i = j = 0;
24     while((c = s[i++]) != '\0') {
25         if((s[i] == '-') && judge(c,s[i + 1])) {
26             i++;
27             while(c < s[i])
28                 t[j++] = c++;
29         } else {
30             t[j++] = c;
31         }
32     }
33     s[j] = '\0';
34 }
35 
36 int main()
37 {
38     char a[1000],b[1000];
39     gets(a);
40     expand(a,b);
41     printf("%s\n",b);
42     return 0;
43 }
原文地址:https://www.cnblogs.com/cpoint/p/3367504.html