标题:求助 字符串展开问题
只看楼主
baolis
Rank: 2
来 自:呼啦瓦星
等 级:论坛游民
帖 子:39
专家分:59
注 册:2021-11-10
结帖率:100%
已结贴  问题点数:20 回复次数:2 
求助 字符串展开问题
#include <stdio.h>
#include <stdlib.h>

/*在保存字符串时,对出现在连续位置的若干个字符,
如这些字符在ASCII表中也是连续出现,则可以将这些字符使用缩写形式进行保存,以节省保存空间。
例如用A-Z表示ABCD...XYZ,1-5表示12345,c-f表示cdef。

输入:包含缩写形式的字符串
输出:将缩写形式进行还原后的字符串
说明:只处理大小写字母和数字,即对于形如A-9,a-C这样的缩写不进行处理
*/

int main(int argc, char *argv[]) {
    static char a[1000],b[1000];
    int i,j;
    gets(a);
    for(i=0,j=0;a[i]!='\0';i++,j++){
        if(a[i]==45){
            if(((a[i-1]>=48&&a[i-1]<=57&&a[i+1]>=48&&a[i+1]<=57)||(a[i-1]>=65&&a[i-1]<=90&&a[i+1]>=65&&a[i+1]<=90)||(a[i-1]>=97&&a[i-1]<=122&&a[i+1]>=97&&a[i+1]<=122))&&(a[i-1]<a[i+1])){
                while(b[j-1]!=a[i+1]-1){
                    b[j]=b[j-1]+1;
                    j++;
                }
                i++;
            }
        }
        b[j]=a[i];   
    }
    puts(b);  

    return 0;
}
已经指针形式编写,小弟不知道咋弄了。希望大神给与帮助
搜索更多相关主题的帖子: 字符 字符串 形式 表示 展开 
2021-11-10 21:51
diycai
Rank: 8Rank: 8
等 级:贵宾
威 望:19
帖 子:147
专家分:895
注 册:2021-5-18
得分:20 
1-2   a-b   紧邻数不处理
1-3-5       解释为12345
程序代码:
#include <conio.h>
void main() 
{
    int map[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
    char prev, cur, next;

    prev = 0;
    while (1)
    {
        cur = getchar();
        if (cur == '-')
        {
            next = getchar();
            if (map[prev] && map[next] && ((prev^next)<' ') && prev+1<next)
            {
                while (prev+1 < next)
                {
                    prev++;
                    printf("%c", prev);
                }
            }
            else
            {
                printf("%c", cur);
            }
            cur = next;
        }
        
        printf("%c", cur);

        if (cur == '\n')
        {
            break;
        }

        prev = cur;
    }
}
2021-11-11 12:36
baolis
Rank: 2
来 自:呼啦瓦星
等 级:论坛游民
帖 子:39
专家分:59
注 册:2021-11-10
得分:0 
谢谢大佬,解答

  printf("Hello, world!\n"); cout << "Hello, world!" << endl;
2021-11-11 13:01



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-507469-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.105838 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved