Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8896413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:02:36+00:00 2026-06-15T00:02:36+00:00

Full disclosure: this is for an assignment. I’m not looking for explicit answers, but

  • 0

Full disclosure: this is for an assignment. I’m not looking for explicit answers, but a little guidance.

I’m having a hard time initializing my stack in C. Specifically, I can’t seem to get it to properly push new elements onto the stack. I know my push/pop/etc functions are correct (they were provided) but I fear that I’m not looking at this correctly.

This is a basic attempt at reading a string and determining if it is “balanced” (all parentheses, curly and square brackets have partners and appear in the correct order.) As far as I can tell, it isn’t a problem with my logic and I believe the syntax is correct, so I’m kind of at a loss for ideas…

Here’s my attempt at the implementation:

int isBalanced(char* s) {

struct DynArr *string;
string = newDynArr(50);

while (nextChar(s) != '\0') {
    if ((nextChar(s) == '(') || (nextChar(s) == '{') || (nextChar(s) == '[')) {
        pushDynArr(string, nextChar(s));
    }
    if (nextChar(s) == ')') {
        if (topDynArr(string) != '(') {
            return 0;
        } else popDynArr(string);
    }
    if (nextChar(s) == '}') {
        if (topDynArr(string) != '{') {
            return 0;
        } else popDynArr(string);
    }
    if (nextChar(s) == ']') {
        if (topDynArr(string) != '[') {
            return 0;
        } else popDynArr(string);
    }
}

if (isEmptyDynArr(string)) {
    printf("The stack is empty\n");
    return 1;
} else return 0;
}

The output always prints “The stack is empty” and returns true, despite me giving it unbalanced strings. I’ve probably been looking at this for too long and can’t recognize the obvious. I’d appreciate any help you can lend. I don’t need explicit answers, but a push in the right direction would be enough.

Edit: Here are the functions that have been requested…

int isEmptyDynArr(DynArr *v) 
{
    if(v->size == 0) {
        return 1;
    }
    else return 0;
}

DynArr* newDynArr(int cap)
{
    assert(cap > 0);
    DynArr *r = (DynArr *)malloc(sizeof( DynArr));
    assert(r != 0);
    initDynArr(r,cap);
    return r;
}

void pushDynArr(DynArr *v, TYPE val)
{
    assert(v != 0);
    addDynArr(v, val);
}

void popDynArr(DynArr *v)
{
    assert(v != 0);
    assert(isEmptyDynArr(v) == 0);
    v->size--;
}

TYPE topDynArr(DynArr *v)
{
    assert(v != 0);
    assert(isEmptyDynArr(v) == 0);
    return v->data[v->size - 1];
}

char nextChar(char* s)
{
    static int i = -1;
    char c;
    ++i;
    c = *(s+i);
    if ( c == '\0' )
        return '\0';
    else
        return c;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T00:02:38+00:00Added an answer on June 15, 2026 at 12:02 am

    this line may skip 1 or 2 or 3 characters from the input line:

    nextChar(s) == '(') || (nextChar(s) == '{') || (nextChar(s) == '['
    

    you should most definitely use:

    char ch = nextChar(s);
    if( ch == '(' || ch == '{' || c == '[' )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

full disclosure - this is for a homework assignment. And I normally would not
Full disclosure: this is for an assignment, so please don't post actual code solutions!
Full disclosure : This is for a homework assignment. This is driving me nuts.
First off, full disclosure: This is going towards a uni assignment, so I don't
Full disclaimer: this is not really a homework, but I tagged it as such
Full disclosure, this is part of a homework assignment (though a small snippet, the
Full disclosure: This is my first time doing any significant programming in C, and
First, in order to provide full disclosure, I want to point out that this
How would you define testing? In the interest of full disclosure, I'm posting this
Full disclosure: this issue is duplicated on the ggplot2 google group I'm developing a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.