Quick Links
University Papers | University Syllabus | Entrance Exam |
PSU Papers | Bank Papers | Placement Papers |
VTU | Anna Univerity Syllabus | Anna Univerity Papers |
Here is the C++ program which would solve the problem given below. The code was compiled and testing for few of the inputs. The validation of the brackets are done using simple logic using IF conditions.
Given a stream of string S of ‘(‘, ’)’, ‘{‘, ‘}’, ‘[‘, ‘]’, determine if the input string is valid i.e. brackets must close in correct order i.e. ‘(‘ with ‘)’, ‘{‘ with ‘}’ and ‘[‘ with ‘]’.
Given a string of length N, output ‘Correct’ if brackets close in correct order else output ‘Incorrect’.
Suggestions are welcomed and in case you have a better program, please post it in the comment section.
For More Programming Codes - CLICK HERE
#include iostream
Given a stream of string S of ‘(‘, ’)’, ‘{‘, ‘}’, ‘[‘, ‘]’, determine if the input string is valid i.e. brackets must close in correct order i.e. ‘(‘ with ‘)’, ‘{‘ with ‘}’ and ‘[‘ with ‘]’.
Given a string of length N, output ‘Correct’ if brackets close in correct order else output ‘Incorrect’.
Suggestions are welcomed and in case you have a better program, please post it in the comment section.
For More Programming Codes - CLICK HERE
#include iostream
#include stdio.h
#include string.h
using namespace std;
#define MAX 100
char str[100];
struct stack{
char holdSymbols[MAX];
int top;
}stackObject;
void pushSymbol(char item)
{
stackObject.top++;
stackObject.holdSymbols[stackObject.top] = item;
}
void popSymbol()
{
stackObject.top--;
}
char* validString(char* input1){
stackObject.top = -1;
int i;
for(i=0;input1[i]!=NULL;i++){
if(input1[i]!=' '){
if (i==0){
pushSymbol(input1[i]);
}else{
if(stackObject.holdSymbols[stackObject.top]=='{' && input1[i]=='}'){
popSymbol();
}else if(stackObject.holdSymbols[stackObject.top]=='(' && input1[i]==')'){
popSymbol();
}else if(stackObject.holdSymbols[stackObject.top]=='[' && input1[i]==']'){
popSymbol();
}else{
pushSymbol(input1[i]);
}
}
}
}
char *output;
if (stackObject.top == -1){
output = "Correct";
}else{
output = "Incorrect";
}
cout << output;
return output;
}
int main()
{
char temp[100]={"({} [((({{}}) [{()}]) )])"};
validString(temp);
return 0;
}
0 Comments