Count the number of repeated strings and the number of repeated strings

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.