Answer to Question #160252 in C for Zenifer

Question #160252

Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.


An input string is valid if:


Open brackets must be closed by the same type of brackets.

Open brackets must be closed in the correct order.


Example 1:


Input: s = "()"

Output: true


Example 2:


Input: s = "()[]{}"

Output: true


Example 3:


Input: s = "(]"

Output: false


Example 4:


Input: s = "([)]"

Output: false


Example 5:


Input: s = "{[]}"

Output: true




Please correct this program for the upper example, please don't change the way of the program which I made. you can add certain functions to solve this program if I miss anything.





#include <stdio.h>

#include <stdlib.h>


// This is where the parentheses are stored in memory

char buffer[1024];

char stack_pop();

void stack_push(char ch);

int stack_size();


int stk_sz = 0;

int str_sz;

char stack[1024];

int checker();


int main(){

FILE *fp;

  // The parentheses sequences is in the parentheses.txt file.

  fp=fopen("parentheses.txt","r");

  if(fp==NULL){

   printf("The input file does not exist.\n");

   exit(-1);

  }

  // Read the parenthese sequences into buffer array.

  fgets(buffer,1024,fp);

  //printf("%s",buffer);


//  puts(buffer);


  str_sz = strlen(buffer);


  if (checker()) {

    puts("true");

  } else puts("false");


  fclose(fp);

}


// The pop operation in the stack. To be done.

char stack_pop(){

char ch = stack[stk_sz - 1];

stk_sz--;


return ch;

}


char peek() {

  return stack[stk_sz - 1];

}


// The push operation in the stack. To be done.

void stack_push(char ch){

  stack[stk_sz] = ch;

  stk_sz++;

}



// The number of elements in the stack. To be done.

int stack_size(){

return stk_sz;

}



int checker() {

  stk_sz = 0;

  int i;

  for ( i = 0; i < str_sz; i++) {

    if (buffer[i] == '(' || buffer[i] == '{' || buffer[i] == '[') { //if there is an opening bracket

      //push it on the stack

      stack_push(buffer[i]);

    } else {

      if (stack_size() == 0) return 0; //if stack is empty, return 0


      switch (buffer[i]) {

      case ')':

        if (peek() != '(') return 0;

        stack_pop();

        break;


      case '}':

        if (peek() != '{') return 0;

        stack_pop();

        break;


      case ']':

        if (peek() != '[') return 0;

        stack_pop();

        break;

      }

    }

  }


  if (stack_size() > 0) return 0; //if stack is not empty, return 0

  return 1;

}


1
Expert's answer
2021-01-31T14:05:15-0500
#include<bits/stdc++.h>
using namespace std;


bool isValid(string str)
{
    stack<char> s;


    for (int i=0; i<str.size(); i++)
    {
        if(str[i]=='(' || str[i]=='{' || str[i]=='[' ){
            if(!s.empty()){
                if(str[i]!=s.top()) return false;
            }
            s.push(str[i]);
        }
        if(str[i]==')' ||str[i]=='}' ||str[i]==']'){
            if(s.empty()) return false;
            char c=s.top();
            s.pop();
            if((c=='(' && str[i]==')' ) || (c=='{' && str[i]=='}' ) ||(c=='[' && str[i]==']' ) ){
                continue;
            }
            else{
              return false;
            }
        }
    }
    if (!s.empty())
        return false;
    else
        return true;
}


int main()
{
    string str;
    scanf(str) ;
    if(isValid(str))
        printf("Valid\n");
    else
        printf("InValid\n)";
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

Zenifer
31.01.21, 20:17

I was asking the program in "C"

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS