Click here to Skip to main content
15,887,350 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i am trying to balance these characters , i receiving some errors , can anyone try to see my code where i need the changes

Error is "Segmentation fault: 11"

What I have tried:

C++
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef char T;
#include "stack.h"

int isBalanced(char s[100])
{
    Stack *stack;
    int n=strlen(s);
    int i;
    for(i=0;i<n;i++)
    {
      if(s[i]=='(' || s[i]=='[' || s[i]=='{')
      {
        Push(&stack,s[i]);
      }
      else if(s[i]==')' || s[i]==']' || s[i]=='}')
      {
        if(IsEmptyStack(stack))
        {
          return 0;
        }
        else if(Top(stack)!=s[i])
          Pop(stack);     
      }
    }
    if(IsEmptyStack(stack))
      return 1;
    else
      return 0;
   

}

int main()
{
    char s1[100]="((()))", s2[100]="((())(";

    if(isBalanced(s1))
      printf("Balanced | \n");
    else
      printf("Not balanced | \n");

    if(isBalanced(s2))
      printf("Balanced | \n");
    else
      printf("Not balanced | \n");

    return 0;
}
Posted
Updated 12-Sep-17 12:28pm
v2
Comments
Dave Kreskowiak 12-Sep-17 16:25pm    
...and the errors would be .... ?? Hint: The really IMPORTANT part you're leaving out of the question.
Patrice T 12-Sep-17 16:46pm    
"i receiving some errors"
Which error messages ?
This code is not complete, we can't run it.
Zubair Farahi 12-Sep-17 16:54pm    
ops! dam sorry a "Segmentation fault: 11"
Zubair Farahi 12-Sep-17 17:03pm    
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef char T;
#include "stack.h"

int isBalanced(char s[100])
{
Stack *stack;
int n=strlen(s);
int i;
for(i=0;i<n;i++)
{
if(s[i]=='(' || s[i]=='[' || s[i]=='{')
{
Push(&stack,s[i]);
}
else if(s[i]==')' || s[i]==']' || s[i]=='}')
{
if(IsEmptyStack(stack))
{
return 0;
}
else if(Top(stack)!=s[i])
Pop(stack);
}
}
if(IsEmptyStack(stack))
return 1;
else
return 0;


}

int main()
{
char s1[100]="((()))", s2[100]="((())(";

if(isBalanced(s1))
printf("Balanced | \n");
else
printf("Not balanced | \n");

if(isBalanced(s2))
printf("Balanced | \n");
else
printf("Not balanced | \n");

return 0;
}
Patrice T 12-Sep-17 17:43pm    
Use Improve question to update your question.
So that everyone can pay attention to this information.

Do you mean
C++
#include <iostream>
#include <cstring>
#include <stack>
using namespace std;

int isBalanced(char s[100])
{
    stack<char> stk;
    int n=strlen(s);
    int i;
    for(i=0;i<n;i++)
    {
      if(s[i]=='(' || s[i]=='[' || s[i]=='{')
      {
        stk.push(s[i]);
      }
      else if(s[i]==')' || s[i]==']' || s[i]=='}')
      {
        if(stk.empty())
        {
          return 0;
        }
        else if(stk.top() != s[i])//<--- What's the purpose of this line?
          stk.pop();
      }
    }
    if(stk.empty())
      return 1;
    else
      return 0;
}


However I can't guess the purpose of the marked line
 
Share this answer
 
Comments
Zubair Farahi 12-Sep-17 17:17pm    
for checking if the top of the stack is not in pair with array of s[i]
Your algorithm is wrong.
When you encounter an opening ([{, you push it on stack, so far, so good.
When you encounter a closing )]}, you check if stack is empty, ok.
then you should check if the closing parenthesis is matching the opening one on stack, otherwise, it is a fail.
if ( is on top of stack, ) is the only one allowed.

You have to rethink your algorithm.
When encountering a, opening parenthesis, I would push the closing counterpart on stack, it simplify later checking.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900