title: RunLength Decoding in C
body: <p>This is my program for RunLength Decoding. But is is giving output as garbage values. The output in the <code>char *decode_rle(char *a,int length)</code> method is correct, but when it is returned to the main function it is wrong. </p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;string.h&gt;
  
  char *decode_rle(char *a,int length)
  {
      char op[50];
      int i,j,k=0,count=0;
      for(i=0;i&lt;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&lt;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;
  }
  </code></pre>