栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

一道C语言练习题

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

一道C语言练习题

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]='';
}





转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/384485.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号