A string coding algorithm for extracting compression

Source: Internet
Author: User

A few days ago, there was a little friend of C who was talking to me about a problem, which he said:

Pass in a string with the following format requirements:

Ep:a2d3b4c1d2

Then the result of extracting this string should be: AADDDBBBBCDD

The rule is, must be satisfied, the first character of the string is a letter, can be uppercase or lowercase, the second is a number, the range is 1-9, the length of the decompression must not exceed 1000 characters.

According to his request, I thought about it, implemented the algorithm, the algorithm is as follows:

#include <stdio.h> #include <stdlib.h> #include <string.h>int main (void) {//char *str = "a3b4c2d1";// Char *str = "a6b9c6d3"; char *str = "A9B9C9D9E9F9G9";  string to be decompressed int str_lenght = 0; int  i;static int count; char *p = Null;char ch;p = malloc (+) while (*str++! = ') The judging string has no pseudo 0 to the last character, and if it does, exit the loop {str_lenght = Atoi (str);//Convert the number in the string to an integer and return to the variable str_lenght  //Why do you do this step? Because I think about *str++//, the position of the pointer is pointing to the second character, minus one is to point to the first character str--;//apply 1000 bytes of memory space for the extracted characters, but this place is not good enough// If the string is not so big then there is no need to engage in such a large space, the optimization can be used in dynamic allocation, how much when the allocation of how much. while (count < 1000) {//This step is the first number in the string that will be fetched, it also represents the number of characters in the string//and then assigns a value to the allocated memory space for (i = 0; i < str_lenght; i++) {P[count] =  *str;//This count corresponds to the coordinates of the array, which is defined as the static type so that count can remember the current position//so that the next time the data is stored back from the current position of +1 (count++);} Do this step to let the pointer address offset +2, this time will be offset to the next character position to Str+=2;break;}} printf ("%s\n", p);//print the extracted data free (p);//release memory return 0;}

Operation Result:



A string coding algorithm for extracting compression

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.