3-3 编写expand(s1,s2)

自己做的练习题,不知道对不对,自学只能做到这个程度,如果有更好的方法,麻烦留我一份,共同进步微笑
#include <stdio.h>	
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
void expand(const char *from, char *to) {
	if (from == NULL || to == NULL) {
		return;
	}
	const char *t = from; 
	char *p = NULL;//用于查找'-'字符的位置
	char b, e;	// 记录'-' 前后字符,用于循环扩展添加
	
	if (*t == '-') { //第一位如果是'-'直接复制
		*to++ = *t++;
	}
	p = strchr(t, '-'); //查找'-'第一次出现的位置
	if (p == NULL) {	//如果没有找到'-'字符 则直接拷贝所有字符,并返回
		while ((*to++ = *t++) != '\0');
		return;
	}
	while (*t != '\0')
	 {
		if (p==NULL||t < p - 1) { //如果找到'-'字符,则拷贝p-1 之前的字符
			*to++ = *t++;
			continue;
		}
		if (p!=NULL && isalpha(*(p - 1)) && isalpha(*(p + 1))) { //判断是否是字母
			if (islower(*(p - 1)) && islower(*(p + 1))) { //判断是否都是大小写
				b = *(p - 1);// 记录 p-1位置的值
				e = *(p + 1);//记录p+1位置的值
				for (;b <= e;++b) {
					*to++ = b; // 循环添加 b-e的字符
				}
				t = (p + 2);  //t 指向e之后的位置
				p = strchr(t, '-');//查找下一'-'
				continue;
			}
			else if (isupper(*(p - 1)) && isupper(*(p + 1))) {
				b = *(p - 1);
				e = *(p + 1);
				for (;b <= e;++b) {
					*to++ = b;
				}
				t = (p+2);
				p = strchr(t, '-');
				continue;
			}
			else
			{
				while (t<=p)
				{
					*to++ = *t++;
				}
				p = strchr(t, '-');
			}
		}
		else if (p != NULL &&isdigit(*(p - 1)) && isdigit(*(p + 1))) {
			b = *(p - 1);
			e = *(p + 1);			
			for (;b <= e;++b) {
				*to++ = b;
			}
			t = (p + 2);
			p = strchr(t, '-');
			continue;
		}
		else
		{
			while (t <= p)
			{
				*to++ = *t++;
			}
			p = strchr(t, '-');
		}
	}

	*to = '\0';
}

int main() { 
	char buf1[] = "--a-b-k0-9A-Z-a-zgdfgggrrte555";
	char buf2[1000];
	expand(buf1, buf2);
	printf("%s\n", buf2);

 	system("pause");
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值