Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
3.3 kB
5
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 current_number = src[index] - '0';
    if (src[index + 1] == '{') {
        depth = 1;
        index += 2;
        return current_number;
    } else {
        int to_add = extractInt(src, ++index, depth);
        return current_number * pow(10, depth);
    }
}


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
    */
    if (repeat_times <= 0) return;
    strcat(repeated_str, str);
    repeatString(str, repeat_times - 1, repeated_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);
    if (index >= len) {
        return;
    }

    char ch = src[index];
    if ((ch <= '9') && (ch >= '0')) {
        int dep = 0;
        int repeat_times = extractInt(src, index, dep);
        
        char part_decoded[1000] = {};
        recursiveDecode(src, index, part_decoded);
        char to_add[1000] = {};
        repeatString(part_decoded, repeat_times, to_add);

        strcat(decoded, to_add);

        if (index < len) {
            char next_part_decoded[1000] = {};
            recursiveDecode(src, index, next_part_decoded);
            strcat(decoded, next_part_decoded);

        }
    } else if (ch == '}') {
        ++index;
        return;
    } else {
        int len_decoded = (unsigned)strlen(decoded);
        decoded[len_decoded++] = src[index++];
        if (src[index + 1] == '}') {
            index += 2;
            return;
        }
        char next_part_decoded[1000] = {};
        recursiveDecode(src, index, next_part_decoded);
        strcat(decoded, next_part_decoded);
    }
}

// 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