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 8319051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T22:11:30+00:00 2026-06-08T22:11:30+00:00

I found several answers to this in C++ or C#, but none for C.

  • 0

I found several answers to this in C++ or C#, but none for C.

I need to know how to count the number of characters in a string. The goal is to determine whether there is a closed block (bounded by braces '{', '}') in a string. right now I have the following:

int closedBlock(char* value) {
    int open = 0;

    int i;
    for (i = 0; i < strlen(value); i++) {
        if (!strcmp("{", value[i])) {
            open++;
        } else if (!strcmp("}", value[i])) {
            open--;
        }
    }

    return !open;
}

but it crashes after on the first if check. I’m not really clear on why this does not work. I imagine it has something to do with bad pointers (that seems to always be the problem in C), but I can’t figure it out. In addition to working code, and explanation of why mine is bad would be greatly helpful.

Thanks.

NOTE

I am aware that this simply check that the number of '{' is equal to the number of '}', and not truly that there is a properly closed block. I’ll solve that problem after I solve this one.

  • 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-08T22:11:33+00:00Added an answer on June 8, 2026 at 10:11 pm

    strcmp will compare two null-terminated strings. It still baffles me that your compiler actually doesn’t mutter about the second parameter being a char. If you want to compare a single character just use the equal-operator ==:

    int closedBlock(char* value) {
        int open = 0;
        int length = strlen(value);
        int i;
        for (i = 0; i < length; i++) {
            if (value[i] == '{') {
                open++;
            } else if (value[i] == '}') {
                open--;
            }
        }
    
        return !open;
    }
    

    Hint: If you work with gcc add -Wall -Wextra to your compiler call, it will often result in useful warnings.

    I am aware that this simply check that the number of ‘{‘ is equal to the number of ‘}’, and not truly that there is a properly closed block. I’ll solve that problem after I solve this one.

    Another hint here: when can there be an invalid block? If and only if the end-token } occurs without a preceding start-token {. You already have all tools for this, you’re just missing another if-statement.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've seen several posts about this, but found no answers to the issue that
I've found several threads on various sites vaguely related this, but no solid answers.
I know this question has been asked several times on SO, but none of
I have found several similar answers but I can't quite get why this is
I found many answers to this question on Google, but none of them seem
First of all, I've know there are several questions targeting this topic, but none
I found several answers and solutions, but most of it somehow integrated server-side helper.
I've looked through several posts but I haven't quite found any answers that have
I have a rather simple question and I have found several answers to this
I have found several answers to this problem, however it is not what i

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.