c语言字符串如何压缩

Python016

c语言字符串如何压缩,第1张

话说B数组不应该是整形呀,不然不能保存字母了。以下是我的代码。。。

#include <iostream>

#include <string.h>

#include <stdio.h>

using namespace std

void yasuo(char a[],char b[])

{

int count=1,p=0

for(int i=0 i<strlen(a) i++)

if(a[i]==a[i+1])

count++

else if(count>2)

{

b[p++]=(char)(count+'0')

b[p++]=a[i]

count=1

}

else if(count==2)

{

b[p++]=a[i]

b[p++]=a[i]

count=1

}

else

b[p++]=a[i]

}

void printB(char b[])

{

cout<<b<<endl

}

void backB(char b[])

{

for(int i=0 i<strlen(b) i++)

if(b[i]<='9'&&b[i]>='3')

{

for(int j=0 j<(int)(b[i]-'0') j++)

cout<<b[i+1]

i++

}

else

cout<<b[i]

cout<<endl

}

int main()

{

char a[1000]= {0},b[1000]= {0}

gets(a)

yasuo(a,b)

printB(b)

backB(b)

}

/*

原串: 111225555

压缩后: 312245

原串: 333AAAbbbb

压缩后: 333A4b

原串: ASXDCdddddd

压缩后: 1A1S1X1D1C6d

Press any key to continue

*/ #include <stdio.h>

#include <string.h>

char *CompressStr(char s[]) {

char t[255]

int i = 0,j,k = 0

while(s[i]) {

j = i + 1

while(s[i] == s[j]) ++j

t[k++] = j - i + '0'

t[k++] = s[i]

i = j

}

t[k] = '\0'

strcpy(s,t)

return s

}

int main(void) {

char i,s[][20] = {"111225555","333AAAbbbb","ASXDCdddddd"}

for(i = 0 i < 3 ++i) {

printf("原串: %s\n",s[i])

printf("压缩后: %s\n",CompressStr(s[i]))

}

return 0

}

#include<stdio.h>

#define MAX_NUM 32int main()

{ char *p,str[60]

int i,j=0

static int num[26]

p=(char*)malloc(MAX_NUM*sizeof(char))

gets(str)

for(i=0str[i]i++)

{ if(str[i]==' ') p[j++]=str[i]

else {

if(num[str[i]-97]==0||num[str[i]-97]==2||num[str[i]-97]==5)

{ p[j++]=str[i] num[str[i]-97]++}

else num[str[i]-97]++

} }

for(i=0i<ji++)

putchar(p[i])

getch()

return 0

}注:输入的为小写字母,而且句子长度不超过60个字符,保存字数不超过32个。。。。。在win_tc中通过……