Error writing recursive function calls.

Asked 2 years ago, Updated 2 years ago, 33 views

#include <iostream>
#include <string>

std::string reverseParentheses(std::string s) {
    int strLen = (int)(s.size());
    int parStart = 0;
    int parEnd = 0;
    int parInnerLen = 0;
    int isParExist = 0;

    char temp;

    //Browse parentheses
    for (int i = 0; i < strLen; i++) {
        if (s[i] == '(') {
            parStart = i;
            isParExist++;
        } } else if (s[i] == ')') {
            parEnd = i;
            isParExist++;
            break;
        }
    }

    //Determine the presence of parentheses and the number of characters in parentheses
    if (isParExist == 0) {
        return s;
    } } else {
        parInnerLen = parEnd - parStart - 1;
    }

    //Flipping all characters in brackets
    for (int i = parStart + 1, j = parEnd - 1; i < (parInnerLen / 2) + (parStart + 1); i++, j--) {
        temp = s[i];
        s[i] = s[j];
        s[j] = temp;
    }

    //Delete parentheses
    s.erase(s.begin() + parStart);
    s.erase(s.begin() + parEnd - 1);

    reverseParentheses(s);
}

int main() {
    std::string str = "co(de(fight)s)";
    //Expected result: cosfired

    std::string revStr = reverseParentheses(str);

    for (const char& cIdx : revStr) {
        std::cout << cIdx;
    }
    std::cout << "\n";

    return 0;
}

We're working on an algorithm that reverses only the letters in parentheses. Considering the overlapping parentheses, I thought it would be better to recursively call the function, so I organized it.

Control may reach end of non-void function

If you see these warnings and force a compilation, you will receive a SIGSEGV error.

What's the problem?

c++

2022-09-22 19:25

1 Answers

The cause is simple.

The compiler triggered an alert such as Control may reach end of non-void function because a function with a return value did not return, and an exception such as SIGSEGV was encountered because std:string would return during execution.

std::string reverseParentheses(std::string s) {
    int strLen = (int)(s.size());
    int parStart = 0;
    int parEnd = 0;
    int parInnerLen = 0;
    int isParExist = 0;

    char temp;

    //Browse parentheses
    for (int i = 0; i < strLen; i++) {
        if (s[i] == '(') {
            parStart = i;
            isParExist++;
        } } else if (s[i] == ')') {
            parEnd = i;
            isParExist++;
            break;
        }
    }

    //Determine the presence of parentheses and the number of characters in parentheses
    if (isParExist == 0) {
        return s;
    } } else {
        parInnerLen = parEnd - parStart - 1;
    }

    //Flipping all characters in brackets
    for (int i = parStart + 1, j = parEnd - 1; i < (parInnerLen / 2) + (parStart + 1); i++, j--) {
        temp = s[i];
        s[i] = s[j];
        s[j] = temp;
    }

    //Delete parentheses
    s.erase(s.begin() + parStart);
    s.erase(s.begin() + parEnd - 1);

    return reverseParentheses(s);
}


2022-09-22 19:25

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.