这是我的运行长度解码程序。但是将输出作为垃圾值提供。方法中的输出char *decode_rle(char *a,int length)
是正确的,但是当它返回到主函数时它是错误的。
#include<stdio.h>
#include<string.h>
char *decode_rle(char *a,int length)
{
char op[50];
int i,j,k=0,count=0;
for(i=0;i<length;i++)
{
if( a[i]=='a' || a[i]=='b' || a[i]=='c' || a[i]=='d' || a[i]=='e' || a[i]=='f' || a[i]=='g')
{
count = a[i+1] - '0';
for(j=0;j<count;j++)
{
op[k]=a[i];
k++;
}
}
}
op[k] = '\0';
printf("\n the decoded string is %s\n",op);
return op;
}
int main()
{
int i=0,j,length,count;
char a[20],*output;
printf("\n Enter a string ");
gets(a);
printf("\n The string you entered is %s",a);
length = strlen(a);
printf("\n length is %d\n",length);
output = decode_rle(a,length);
i=0;
while(output[i]!='\0')
{
printf("%c",output[i]);
i++;
}
getch();
return 0;
}