Count the number of repeated strings and the number of repeated strings
Question: Count and output the number of repeated strings.
Example input: abcdef
Sample output: a1b1c1d1e1f1
Example input: abbbbbbbbbbbbbcc
Sample output: a1b11c2
The implementation code is as follows:
# Ifndef STRREPEAT_H # define STRREPEAT_H # include <string. h >#include <stack >#include <iostream> void StrRepeat (char * str) {if (str = NULL) return; char * pre = str; // point to the previous character char * cur = (str + 1); // point to the next character char * result = new char [strlen (str) * 2]; // apply for the storage result space int k = 0; int times = 1; // The initial value is 1std: stack <int> bitStack; // It is used to process times> 10 while (* cur! = '\ 0') {// if (* cur = * pre) of the loop condition // equal the number of times + ++ times; else {// handle case of inequality result [k ++] = * pre; while (times! = 0) {// processing times> 10 bitStack. push (times % 10); times = times/10;} while (! BitStack. empty () {result [k ++] = bitStack. top () + '0'; bitStack. pop () ;}times = 1; // reset times} pre = cur; // move the pointer behind cur = cur + 1;} result [k ++] = * pre; // process the last character because the last character is not processed while (times! = 0) {// processing times> 10 bitStack. push (times % 10); times = times/10;} while (! BitStack. empty () {result [k ++] = bitStack. top () + '0'; bitStack. pop ();} result [k] = '\ 0'; // null character std: cout <result <std: endl;} void TestStrRepeat () {char str [100]; while (std: cin >>str) {StrRepeat (str) ;}# endif
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.