扩展字符,如将A-Z,扩展为ABCD......XYZ

 

#include <stdio.h>

#include <string.h>

 

void expand(char * s1, char * s2);

 

int main(void)

{

    char *s[] = { "a-z-", "z-a-", "-1-6-",

                  "a-ee-a", "a-R-L", "1-9-1",

                  "5-5", NULL };

    char result[100];

    int i = 0;

    

    while ( s[i] ) {

       

        /*  Expand and print the next string in our array s[]  */

       

        expand(result, s[i]);

        printf("Unexpanded: %s\n", s[i]);

        printf("Expanded  : %s\n", result);

        ++i;

    }

   

    return 0;

}

void expand(char * s1, char * s2)

{

    static char upper_alph[27] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    static char lower_alph[27] = "abcdefghijklmnopqrstuvwxyz";

    static char digits[11]     = "0123456789";

   

    char * start, * end, * p;

    int i = 0;

    int j = 0;

      

    /*  Loop through characters in s2  */

   

    while ( s2[i] )

       {

        switch( s2[i] )

              {

                 case '-':

            if ( i == 0 || s2[i+1] == '\0' )//'-'出现在字符串的开头或结尾

                     { 

                s1[j++] = '-';

                ++i;

                break;

            }

            else

                     { 

              //大写字符扩展,start返回'-'前一个字符所在字符串中的位置,end返回'-'后一个字符所在字符串中的位置             

                if ( (start = strchr(upper_alph, s2[i-1])) &&(end = strchr(upper_alph, s2[i+1])) )

                    ;

              //小写字符扩展,start返回'-'前一个字符所在字符串中的位置,end返回'-'后一个字符所在字符串中的位置  

                else if ( (start = strchr(lower_alph, s2[i-1])) &&(end = strchr(lower_alph, s2[i+1])) )

                    ;

                            //数字扩展

                else if ( (start = strchr(digits, s2[i-1])) &&(end = strchr(digits, s2[i+1])) )

                    ;

                else //没有找到

                            {                    

                    fprintf(stderr, "EX3_3: Mismatched operands '%c-%c'\n",s2[i-1], s2[i+1]);

                    s1[j++] = s2[i-1];

                    s1[j++] = s2[i++];

                    break;

                }

                /*  Expand the range  */

                p = start;

                            //扩展字符

                while ( p != end )

                            {

                    s1[j++] = *p;

                    if ( end > start )

                        ++p;

                    else

                        --p;

                }

                s1[j++] = *p;

                i += 2;

            }

            break;

           

        default:

            if ( s2[i+1] == '-' && s2[i+2] != '\0' )

                     {/*  This character is the first operand in

                    a range, so just skip it - the range will

                    be processed in the next iteration of

                    the loop.                                   */

                 ++i;

            }

            else

                     {/*  Just a normal character, so copy it  */

                s1[j++] = s2[i++];

            }

            break;

        }

    }

    s1[j] = s2[i];    /*  Don't forget the null character  */

}

//strchr(s,c)在s指向的字符串中查找c,若找到,返回指向它的第一次出现的位置的指针,否则返回NULL

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值