不废话,直接上代码。。。
#include<stdio.h>
#include<string.h>#include<stdlib.h>
#define maxsize 20
void filter(const char*str1, int length, char* str2)
{
bool hash[26] = { 0 };
int i, j;
for (i = 0, j = 0; i < length; i++)
{
if (hash[str1[i] - 'a'] == false)
{
hash[str1[i] - 'a'] = true;
str2[j++] = str1[i];
}
str2[j] = '\0';
}
}
void zip(const char* str1, int length, char* str2)
{
int i, j, k, num;
char buff[20];
for (i = 0, k = 0; i < length; i++)
{
num = 0;
for ( j = i + 1; i < length; ++j)
{
if (str1[i] == str1[j])
num++;
else
break;
}
if (num != 0)
{
memset(buff, 0, sizeof(buff));//将buff 初始化:
_itoa_s(num + 1, buff, 10);
strcpy_s(str2+k,sizeof(str2+k), buff);
k += strlen(buff);
}
str2[k++] = str1[i];
i = i + num + 1;
}
str2[k] = '\0';
}
int main()
{
char str1[] = { "aaebbc" };
char str2[] = { "aabaa" };
char str3[maxsize] = { 0 };
char str4[maxsize] = { 0 };
filter(str1, strlen(str1), str3);
zip(str2, strlen(str2), str4);
puts(str4);
puts(str3);
return 0;
}