您的位置:首页 > 编程语言 > C语言/C++

华为机试题—字符串压缩 c++

2016-07-08 21:33 369 查看
/*
将字符串压缩
通过键盘输入一串小写字母(a~z)组成的字符串。
请编写一个字符串过滤程序,若字符串中出现多个相同的字符,将非首次出现的字符过滤掉
*/
#include <iostream>
#include<string>
using namespace std;
void stringZip(const char *pInputStr, long lInputLen, char *pOutputStr)
{
int i=0,j=0;
int m=0;
int n=0;
char temp[50];
//cout<<strlen(pInputStr);
while(pInputStr[i]!='\0')
{
for(j=i+1;;j++)
{
if(pInputStr[j]!=pInputStr[j-1])
{
m=j-i;
itoa(m,temp,10);
if(m>1){
for(int t=0;temp[t]!='\0';t++)
pOutputStr[n++]=temp[t];
}
pOutputStr[n++]=pInputStr[i];
j--;
break;
}
}
i=j;
i++;
}

}

int main()
{
//char input[100];
char* input = new char[100];
//cin.getline(input,100);
gets(input);
char out[100]="\0";
int n=20;//最大长度
stringZip(input,n,out);
int len=strlen(out);//并不是预先设定的大小
for(int i=0;i<len;i++)
cout<<out[i];
cout<<endl;
delete []input;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: