免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1374 | 回复: 0
打印 上一主题 下一主题

新手学《The C Programming language》,自己做课后的习题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-06-20 11:01 |只看该作者 |倒序浏览
本帖最后由 xiaowh00 于 2012-06-20 11:26 编辑

Exercise 3-3. 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. Arrange that a leading or trailing - is taken literally.


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX 100
/* author: xiaowh0 */
/* date: 2012-6-20 */

/*  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. Arrange that a leading or trailing - is taken literally */

void expand(char[],char[]);
main()
{
        int i,c;
        char s1[MAX],s2[MAX];
        i=0;
        while((c=getchar())!=EOF)
        {
                if(c!='\n')
                        s1[i++]=c;
        }
        s1='\0';
        expand(s1,s2);
        printf("%s\n",s2);
}

void expand(char s1[],char s2[])
{
        int i,j;
        i=j=0;
        while(s1!='\0')
        {
                if(s1>='a' && s1<='z')
                {
                        s2[j]=s1;
                        if(s1[i+1]=='-' && s1[i+2]!='\0')
                        {
                                i=i+2;
                                if(s2[j]>s1 || (s1<'a' || s1 > 'z'))
                                {
                                        printf("You use range %c must large than %c\n",s1,s2[j]);
                                        exit(1);
                                }
                                while(s2[j]<s1)
                                {
                                        s2[j+1]=s2[j]+1;
                                        j++;
                                }
                        }
                        else
                        {
                                i++;
                                j++;
                        }
                }
                else if(s1>='A' && s1<='Z')
                {
                        s2[j]=s1;
                        if(s1[i+1]=='-' && s1[i+2]!='\0')
                        {
                                i=i+2;
                                if(s2[j]>s1 || (s1<'A' || s1>'Z'))
                                {
                                        printf("You use range %c must large than %c\n",s1,s2[j]);
                                        exit(1);
                                }
                                while(s2[j]<s1)
                                {
                                        s2[j+1]=s2[j]+1;
                                        j++;
                                }
                        }
                        else
                        {
                                i++;
                                j++;
                        }
                }
                else if(s1>='0' && s1<='9')
                {
                        s2[j]=s1;
                        if(s1[i+1]=='-' && s1[i+2]!='\0')
                        {
                                i=i+2;
                                if(s2[j]>s1 || (s1<'0' || s1>'9'))
                                {
                                        printf("You use range %c must large than %c\n",s1,s2[j]);
                                        exit(1);
                                }
                                while(s2[j]<s1)
                                {
                                        s2[j+1]=s2[j]+1;
                                        j++;
                                }
                        }
                        else
                        {
                                i++;
                                j++;
                        }
                }
                else
                {
                        s2[j]=s1;
                        i++;
                        j++;
                }
        }
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP