Algorithm question: compress arbitrary strings and compress strings with Algorithms
// Input a string on the keyboard. // Compile a character string compression program to compress the repeated characters in the character string and output the compressed character string. /* For example, input AABBCC, and output 2A2B2C; input,..., and output 3, 2 .. */# Include <iostream> # include <string. h> # include <stdlib. h> using namespace std; void Grial (char * src) {char * Pnext = src; char * Prve = src; // If I select it, modify it on the original string, use the speed pointer to avoid extra space. Char * p = Prve; int count = 0; while (* Pnext! = '\ 0') {char ch = * Pnext; while (ch = * Pnext) {count ++; Pnext ++ ;} char * s = new char [count]; itoa (count, s, 10); count = 0; while (* s) {* Prve = * s; Prve ++; s ++;} * Prve ++ = ch;} * Prve = '\ 0';} int main () {char cinbuff [255]; cin> cinbuff; grial (cinbuff); cout <cinbuff <endl; return 0 ;}
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.