自己做的练习题,不知道对不对,自学只能做到这个程度,如果有更好的方法,麻烦留我一份,共同进步
#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;
}