首页 > 其他 > 详细

重复字符的压缩

时间:2017-06-06 21:16:00      阅读:186      评论:0      收藏:0      [点我收藏+]

  对输入字符串进行压缩,输入"aaabcccdde",输出"3ab3c2de",即对连续出现的字符进行压缩。

#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
char* stringCompress(char a[])
{
    int mark=0;
    char temp=a[0];
    int len=strlen(a);
    //int count[len]={0};
    int count=1;
    //string str;
    char b[len+1];
    int j=0;
    for(int i=1;i<len;i++)
    {
        if(a[i]==temp)
            count++;
        else{
            if(count!=1)
              b[j++]=count+0;
             b[j++]=temp;
             count=1;
            temp=a[i];
        }
    }
    if(count!=1)
      b[j++]=count+0;
    b[j]=temp;
    b[++j]=\0;

    return b;
}
int main()
{
    char a[100];
    cin.getline(a,100);
    cout<<a<<endl;

    char* res=stringCompress(a);
    cout<<res<<endl;
    system("pause");
    return 0;
}

 

重复字符的压缩

原文:http://www.cnblogs.com/wft1990/p/6953409.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!