im intending to make a brackets checking code that will check some code and look if there's any brackets mistakes in it,
i started like this :

#include<stdio.h>

int i=0;

int isPaired(char c) /*checks if bracket matches*/
{
  if (c == '}' && s[--i] == '{')
    return 1;
  if (c == ']' && s[--i] == '[')
    return 1;
  if (c == ')' && s[--i] == '(')
    return 1;
  else
    return 0;
}

int main()
{

  int c;
  char s[100];
  while((c=getchar())!=EOF)
{
    for (;c!='\n',c!=EOF;c=getchar())
    {
        s[i]=c;
    }

    c=getchar();
    if (c=='\"')
    if (c == '{' || c == '[' || c == '(')
    {
        s[i]=c;
        ++i;
    }
    if (c == '}' || c == ']' || c == ')')
        if (s[i]=!'\n' && isPaired(c))
        {
            s[i-1]='\0';
            --i;
        }

}           
    return 0;
}

the task i got is : that i need to check only for brackets mistakes in the code that this program is gonna run on and print out the sentence which the mismatched bracket is at.
and sentence size is no more than 100 characters, in addition to that, every couple of " () " and " [] " brackets are positioned in the same sentence
and ofc exlcuding brackets that's commented or like ")"
may i have someone's guidance...
one thing i know is that i should store the whole sentence in an array thats where im stuck at and dont know how to continue..

the link is dead, it shows an error from the HOST

@rproffitt, Thanks !
And i think i've got the hang of it, but i keept getting errors when i compile my code...im using obunto operating system.
and this is my code:

#include<stdio.h>
#define LEN 100

enum status {OUT,IN_STRING,LEFT_SLASH,IN_COMMENT,RIGHT_STAR};

int s[LEN]={'\0'};
int sp=0;   /* Stack pointer */

void push(char c)  /* Pushes a character in stack */
{
  s[sp++]=c;
}

char pop(void)   /* Pops a character from stack */
{
  s[sp--]=0;
}

char peek(char c)   /* Returns last character in stack */
{
  char z=[sp-1];
  return z;
}

int isPaired(char c) /*Checks if brackets match*/
{
  if (c == '}' && peek() == '{')
    return 1;
  if (c == ']' && peek() == '[')
    return 1;
  if (c == ')' && peek() == '(')
    return 1;
  else
    return 0;
}

int main()
{

  int state=OUT;
  int c;
  int line=1;  /* Line number */
  char str[LEN]={'\0'};   /* characters array to store the current code sentence */
  int i=0;   /* str[] pointer*/

  while((c=getchar())!=EOF)
{
    if (c=='\n') {  /* Resets str[] when start reading a newline */
        ++line;
        for (i=0; i<LEN && str[i]!=('\0'||0); i++)
            str[i]='\0';
        i=0;
    }
    if (c!='\n')   /* Stores characters to print sentence */
        str[i++]=c;

    switch (state)   /* Determines wether the character is within a comment or a string */
    {
    case OUT:
        if (c=='/')
            state=LEFT_SLASH;
        else {
            if (c=='\"')
            state = IN_STRING;
        }
        break;
    case LEFT_SLASH:
        if (c=='*')
            state=IN_COMMENT;
        else {
            state=OUT;
        }
        break;
    case IN_COMMENT:
        if (c=='*')
            state=RIGHT_STAR;
        break;
    case RIGHT_STAR:
        if (c== '/')
            state=OUT;
        else if (c!='*')
            state=IN_COMMENT;
        break;
    case IN_STRING:
        if (c=='\"')
            state = OUT;
        break;
    }    /* End of switch */

    if (state = OUT) {
        if (c == '{' || c == '[' || c == '(')
            push(c);
        else if (c == '}' || c == ']' || c == ')')
            if (isPaired(c))
                pop();
            else {
                printf("In line: %d :\n %s \n there's a mismatching of %c \n",line,str,c);
            }
    }   /* End of first if */

}    /* End of while */         
    return 0;
}

and i get this error when compiling ...(attached image)

@rproffitt, oke i just did those little things noticed them now haha, but still it doesn't work properly,
when i run it for example on this "(){}" it still giving the printf() at the end anyway.. why is that happening
here's the fixed code:

#include<stdio.h>
#define LEN 100

enum status {OUT,IN_STRING,LEFT_SLASH,IN_COMMENT,RIGHT_STAR};

int s[LEN]={'\0'};
int sp=0;   /* Stack pointer */

void push(char c)  /* Pushes a character in stack */
{
  s[sp++]=c;
}

void pop(void)   /* Pops a character from stack */
{
  s[sp--]=0;
}

char peek(void)   /* Returns last character in stack */
{
  char c=s[sp-1];
  return c;
}

int isPaired(char c) /*Checks if brackets match*/
{
  if (c == '}' && (peek() == '{'))
    return 1;
  if (c == ']' && (peek() == '['))
    return 1;
  if (c == ')' && (peek() == '('))
    return 1;
  else
    return 0;
}

int main()
{

  int state=OUT;
  int c;
  int line=1;  /* Line number */
  char str[LEN]={'\0'};   /* characters array to store the current code sentence */
  int i=0;   /* str[] pointer*/

  while((c=getchar())!=EOF)
{
    if (c=='\n') {  /* Resets str[] when start reading a newline */
        ++line;
        for (i=0; i<LEN && str[i]!=('\0'||0); i++)
            str[i]='\0';
        i=0;
        continue;
    }
    if (c!='\n')   /* Stores characters to print sentence */
        str[i++]=c;

    switch (state)   /* Determines wether the character is within a comment or a string */
    {
    case OUT:
        if (c=='/')
            state=LEFT_SLASH;
        else {
            if (c=='\"')
            state = IN_STRING;
        }
        break;
    case LEFT_SLASH:
        if (c=='*')
            state=IN_COMMENT;
        else {
            state=OUT;
        }
        break;
    case IN_COMMENT:
        if (c=='*')
            state=RIGHT_STAR;
        break;
    case RIGHT_STAR:
        if (c== '/')
            state=OUT;
        else if (c!='*')
            state=IN_COMMENT;
        break;
    case IN_STRING:
        if (c=='\"')
            state = OUT;
        break;
    }    /* End of switch */

    if (state == OUT) {
        if (c == '{' || c == '[' || c == '(')
            push(c);
        else if (c == '}' || c == ']' || c == ')') {
            if (isPaired(c))
                pop();
            }
            else {
                printf("In line: %d :\n %s \n there's a mismatching of %c \n",line,str,c);
            }

    }   /* End of first if */

}    /* End of while */         
    return 0;
}

Line 99: Replace with this line to see what is going on...

printf("A non-bracket was entered\n", line, str, c);

when i run it for example on this "(){}" it still giving the printf() at the end anyway..

"Works" for me. I don't get that printout. Can't see why you would either, regardless of brackets entered. See earlier comment.

Your indentation is off, so the if's and the else's and the brackets that you may THINK go with each other likely do not. Here is your code, bracketed to make things a little more obvious of what is really going on.

        if (state == OUT) 
        {
            if (c == '{' || c == '[' || c == '(')
            {
                push(c);
            }
            else if (c == '}' || c == ']' || c == ')')
            {
                if (isPaired(c))
                {
                    pop();
                }
            }
            else 
            {
                printf("In line: %d :\n %s \n there's a mismatching of %c \n", line, str, c);
            }
        }   /* End of first if */

Line 50:

            for (i = 0; i<LEN && str[i] != ('\0' || 0); i++)

I am almost certain this is not what you meant to write.

('\0' || 0) is the same as (0 || 0), which is "false or false", which is the same as "false", which is the same as 0, which is the same as '\0'. To the compiler. It ends up being the same as...

            for (i = 0; i<LEN && str[i] != '\0'; i++)

which is I think what you want, but which makes more sense than what you wrote. You are comparing str[i] to the C-String NULL terminator here, I believe. Right? I believe that by dumb luck what you wrote and what you want ended up being the same thing.

After changing the printf placement, I ran your program with a single opening bracket and pressed enter. No closing bracket. No "mismatching" printout displayed. Should it have?

I think you need to trace through your code to see why it does what it does. If you had a question about one or two lines in particular to look at I would look but 100?

So, in your IDE set break points and if no IDE, use print statement to reveal what's inside at that point.

So, in your IDE set break points

Set them at lines 96 and 99, then if NEITHER of them execute when you're positive exactly one should, look at your brackets/indentation.

After changing the printf placement, I ran your program with a single opening bracket and pressed enter. No closing bracket. No "mismatching" printout displayed.

i've fixed that i think with putting the entire character checking code in a for loop:

  for (i=0; i<LEN && str[i]!=0; i++)
  {
    switch (state)   /* Determines wether the character is within a comment or a string */
    {
    case OUT:
        if (str[i]=='/')
            state=LEFT_SLASH;
        else {
            if (str[i]=='\"')
            state = IN_STRING;
        }
        break;
    case LEFT_SLASH:
        if (str[i]=='*')
            state=IN_COMMENT;
        else {
            state=OUT;
        }
        break;
    case IN_COMMENT:
        if (str[i]=='*')
            state=RIGHT_STAR;
        break;
    case RIGHT_STAR:
        if (str[i]== '/')
            state=OUT;
        else if (str[i]!='*')
            state=IN_COMMENT;
        break;
    case IN_STRING:
        if (str[i]=='\"')
            state = OUT;
        break;
    }    /* End of switch */

    if (state == OUT) 
    {
        if (str[i] == '{' || str[i] == '[' || str[i] == '(')
        {
            push(str[i]);
            continue;
        }
        if (str[i] == '}' || str[i] == ']' || str[i] == ')') 
        {

            if (isPaired(str[i]))
            {
                pop();
                continue;
            }
            else
            {
                printf("In line: %d,\n %s \n Wrong use of bracket: %c \n",line,str,str[i]);
            }
        }

    }        /* End of state check if */
  }        /* End of for */
    ++line;  /* line pointing */

    for (i=0; i<LEN && str[i]!=0; i++)
    {
        str[i]=0;
    }

    i=0; /* resets str[] pointer */

but i can't get it why it doesnt work properly with :

main()
{
    [(])
}

it runs the printf with the "}" last bracket, which isn't supposed to be becuase there is still a matching "{" in the stack... i can't seem to get where's the error in my code...

@rproffitt,

a question about one or two lines in particular to look at I would look but 100?

sorry im quite the beginner in coding and confusion makes me unsure of how to ask a question so i've threw it all at once...... thank you for your paitence sir

Quick look at isPaired suggests that the closing bracket must be immediately after the opening bracket to be detected?

int isPaired(char c) /*Checks if brackets match*/
{
    if (c == '}' && (peek() == '{'))
        return 1;
    if (c == ']' && (peek() == '['))
        return 1;
    if (c == ')' && (peek() == '('))
        return 1;
    else
        return 0;
}

I think it's time to put aside the code and work on the actual algorithm. Pencil and paper. I for one can't tell what the algorithm is, so I can't tell where the code is wrong. Also, not sure where exactly to paste those 66 lines and what they replace.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.