Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Result:
- String entered: AAABBCAABDDAFF
- String encode: 3A2B1C2A1B2D1A2F
- string decode: AAABBCAABDDAFF
- */
- #include <stdio.h>
- #include <string.h>
- int main()
- {
- char str[]="AAABBCAABDDAFF";
- char str_encode[strlen(str)];
- int count=1,i,j=0;
- //encode
- printf("\n String entered: %s",str);
- printf("\n String encode: ");
- for(i=0;i<=strlen(str)-1;i++){
- if(str[i] == str[i+1]){
- count++;
- }else{
- str_encode[j++] = count;
- str_encode[j++] = str[i];
- printf("%d%c",count,str[i]);
- count=1;
- }
- }
- //decode
- int temp;
- printf("\n string decode: ");
- for(i=0;i<=j-1;i+=2){
- int f;
- for(f=0;f<=(int)str_encode[i]-1;f++){
- printf("%c",str_encode[i+1]);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement