Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
3.0 kB
4
Indexable
Never
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdio>
using namespace std;

int extractInt(const char src[], int& index, int& depth) {
    /*
    **************************Task-1***************************
    TODO: Given a string consisting of numeric charaters and English letters (e.g,"10{aaaa}"), return the integer value of the numeric charater(s) (e.g, 10 in this case) with recursive approach.

    Input:
        const char src[]: char array
        int& index: the index of array
        int& depth: the recursion depth
    Output:
        return the int value of the the numeral in the char array
    */
    int ans = 0;
    while (src[index] != '{') {
        int current_number = src[index] - '0';
        ans = ans * 10 + current_number;
        ++index;
    }
    return ans;
}


void repeatString(char str[], int repeat_times, char repeated_str[]) {
    /*
    **************************Task-2***************************
    TODO: implement a function to Recursively repeat the input string
    Input:
        char str[]: char array
        int repeat_times: the repeated times of str
        char repeated_str[]: the repeated char array to be return
    Output:
        void, str string is copied into char repeated_str
    */
    for (int i = 0; i < repeat_times; ++i) {
        strcat(repeated_str, str);
    }
}


void recursiveDecode(const char src[], int& index, char decoded[])
{
    /*
    **************************Task-3***************************
    TODO: implement a function to Recursively generate a new string by removing
          the braces and repeat the string inside braces.
    Input:
        char src[]: char array
        int& index: the index of array
        char decoded[]: the decoded string to be return
    Output:
        void, the decoded string is copied into char decoded[]
    */

    int len = strlen(src);
    int depth = 0;
    while (index < len) {
        char ch = src[index];
        if ((ch <= '9') && (ch >= '0')) {
            int repeat_times = extractInt(src, index, depth);
            char part_decoded[1000] = {};
            recursiveDecode(src, index, part_decoded);
            char to_add[1000] = {};
            repeatString(part_decoded, repeat_times, to_add);
            strcat(decoded, to_add);
        } else if ((ch >= 'a') && (ch <= 'z')) {
            int len_decoded = (unsigned)strlen(decoded);
            decoded[len_decoded++] = src[index++];
        } else if (ch == '{') {
            ++depth;
            ++index;
        } else if (ch == '}') {
            ++index;
            if (depth == 1) {
                return;
            }
            --depth;
        }
    }
}

// Driver program
int main()
{
    char s[1000]={};
    char decoded[1000] = {};
    int index = 0;

    // cout << "input string: " << endl;
    cin >> s;
    recursiveDecode(s, index, decoded);
    // cout << "output string: " << endl;
    cout << decoded << endl;
    return 0;
}
Leave a Comment