Advertisement
yoyolove

en/de-code string

Aug 4th, 2011
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.87 KB | None | 0 0
  1. /*
  2. Result:
  3.  
  4.  String entered: AAABBCAABDDAFF
  5.  String encode: 3A2B1C2A1B2D1A2F
  6.  string decode: AAABBCAABDDAFF
  7.  
  8.  */
  9.  
  10. #include <stdio.h>
  11. #include <string.h>
  12.  
  13. int main()
  14. {
  15.     char str[]="AAABBCAABDDAFF";
  16.  
  17.     char str_encode[strlen(str)];
  18.     int count=1,i,j=0;
  19.     //encode
  20.     printf("\n String entered: %s",str);
  21.     printf("\n String encode: ");
  22.     for(i=0;i<=strlen(str)-1;i++){
  23.         if(str[i] == str[i+1]){
  24.              count++;
  25.         }else{
  26.             str_encode[j++] = count;
  27.             str_encode[j++] = str[i];
  28.             printf("%d%c",count,str[i]);
  29.             count=1;
  30.             }
  31.     }
  32.  
  33.     //decode
  34.     int temp;
  35.     printf("\n string decode: ");
  36.     for(i=0;i<=j-1;i+=2){
  37.             int f;
  38.             for(f=0;f<=(int)str_encode[i]-1;f++){
  39.                 printf("%c",str_encode[i+1]);
  40.             }
  41.     }
  42.  
  43.     return 0;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement