C语言程序设计 6-3 exercise 3-3 (10分) Write a function expand(s1,s2) that expands shorthand notations like

C语言程序设计 第二版 第三章
第三章课后实战,完成全部的函数题(70分);自己选择编程题完成40分即可。

剩余时间:15天
6-3 exercise 3-3 (10分)
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 []);
裁判测试程序样例:
#include <stdio.h>
#include <assert.h>
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("%s\n", s2);
}

return 0;

}

/* 请在这里填写答案 */
输入样例:
a-z
A-A
0-9
c–z
输出样例:
abcdefghijklmnopqrstuvwxyz
A
0123456789
c–z

int st(char c);
void expand(char s1[], char s2[])
{
int j = 0, k = 0;
int dd = 7;
while (s1[j] != ‘\0’)
{
if ( (s1[j] == ‘-’)&&(j!=0) )//后面是 --aa 这种情况 即第一个字符是 - 它前面没有字符了
{
if ( (st(s1[j - 1]) == st(s1[j + 1]))&& (st(s1[j - 1]) !=4) )
{
dd = s1[j + 1] - s1[j - 1];
if (dd < 0) {
s2[k] = s1[j];
k++; j++;
}
else
{
for (; s2[k - 1] < (s1[j + 1] - 1); )
{
s2[k] = s2[k - 1] + 1;
k++;
}
if (!dd) j += 2;// 此处是为了 解决 --A 这种情况
else
j++;
}
}

		else
		{
			s2[k] = s1[j];
			j++; k++;
		}
	}
	else
	{ 
		s2[k] = s1[j];
		j++; k++;
	}
	dd = 7;
}
s2[k] = '\0';

}

int st(char c)
{
if (c >= ‘0’ && c <= ‘9’)
return 1;
else if (c >= ‘a’ && c <= ‘z’)
return 2;
else if (c >= ‘A’ && c <= ‘Z’)
return 3;
else
return 4;状态已经指明了
}

  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值