#include <iostream>
#include <string>
#include <stdlib.h>
#include<stdafx.h>
#define TEXT_SIZE 1024
using namespace std;
char* Zip(char*);
int main()
{
char text[TEXT_SIZE];
cout<<"Please input the text to be zipped:";
cin>>text;
char* zippedText = Zip(text);
cout<<"The zipped text is: "<<zippedText<<endl;
system("PAUSE");
return 0;
}
char* Zip(char *text)
{
//Èç¹ûÎı¾Îª¿Õ£¬Ôò·µ»Ø¿Õ
if (!text)
return NULL;
//Ö¸ÏòtextÖеÄ×Ö·ûµÄÖ¸Õë
char *preChar = text;
char *curChar = &text[1];
//´¢´æѹËõÒÔºóµÄÎı¾×Ö·ûÊý×é
char tempText[TEXT_SIZE];
//Ö¸ÏòtempTextÖеÄij¸ö×Ö·ûµÄÖ¸Õë
char *tempTextPtr = tempText;
//×Ö·ûÖظ´´ÎÊý¼ÆÊýÆ÷
int countChar = 1;
tempTextPtr[0] = preChar[0];
tempTextPtr++;
while(curChar[0] != '/0')
{
if (curChar[0] != preChar[0])
{
preChar = curChar;
if (countChar != 1)
{
tempTextPtr[0] = static_cast<char>(countChar + '0');
tempTextPtr++;
}
tempTextPtr[0] = curChar[0];
tempTextPtr++;
countChar = 1;
}
else
countChar++;
curChar++;
}
if (countChar != 1)
{
tempTextPtr[0] = static_cast<char>(countChar + '0');
tempTextPtr++;
}
tempTextPtr[0] = '/0';
return tempText;
}