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
*Your program output should follow the example in the instruction.
* The program should print the input parentheses and the output (true/false).
*Parentheses.txt : ((()))
Please Follow this Program to Solve the Parentheses:
#include <stdio.h>
#include <string.h>
#define mx 1000000 //max size of 10^6
int str_sz;
char str[mx];
int stk_sz;
char stack[mx];
int checker() {
stk_sz = 0;
for (int i = 0; i < str_sz; i++) {
if (str[i] == '(' || str[i] == '{' || str[i] == '[') { //if there is an opening bracket
//push it on the stack
stack[stk_sz] = str[i];
stk_sz++;
} else {
if (stk_sz == 0) return 0; //if stack is empty, return 0
switch (str[i]) {
case ')':
if (stack[stk_sz - 1] != '(') return 0;
stk_sz--;
break;
case '}':
if (stack[stk_sz - 1] != '{') return 0;
stk_sz--;
break;
case ']':
if (stack[stk_sz - 1] != '[') return 0;
stk_sz--;
break;
}
}
}
if (stk_sz > 0) return 0; //if stack is not empty, return 0
return 1;
}
int main() {
scanf("%s", str);
str_sz = strlen(str);
if (str_sz == 0) {
puts("String is empty");
return 0;
}
if (checker()) puts("Balanced");
else puts("Not balanced");
}
You are required to complete this Parentheses program template I provide and use the stack to solve the problem. Please test the program.
#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 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);
}
// The pop operation in the stack. To be done.
char stack_pop(){
return ')';
}
// The push operation in the stack. To be done.
void stack_push(char ch){
}
// The number of elements in the stack. To be done.
int stack_size(){
return 0;
}
#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 main(int argc, char* argv[]){
FILE *fp;
char *pch;
char ch;
int correct = 1;
// The parentheses sequences is in the parentheses.txt file.
if (argc > 1) {
fp=fopen(argv[1],"r");
}
else {
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("Input: s = \"%s\"\n",buffer);
for (pch = buffer; *pch != '\0'; pch++ ) {
if (*pch == '(' || *pch == '[' || *pch == '{') {
stack_push(*pch);
}
else if (stack_size() == 0 ) {
correct = 0;
break;
}
else {
ch = stack_pop();
if (ch == '(' && *pch != ')') {
correct = 0;
break;
}
if (ch == '[' && *pch != ']') {
correct = 0;
break;
}
if (ch == '{' && *pch != '}') {
correct = 0;
break;
}
}
}
if (correct) {
printf("Output: true\n");
}
else {
printf("Output: false\n");
}
return 0;
}
char stack[1024];
int top=0;
// The pop operation in the stack. To be done.
char stack_pop(){
return stack[--top];
}
// The push operation in the stack. To be done.
void stack_push(char ch){
stack[top++] = ch;
}
// The number of elements in the stack. To be done.
int stack_size(){
return top;
}
Comments
Leave a comment