Write a program called balanced that does the following: Prompts the user for a
ID: 649167 • Letter: W
Question
Write a program called balanced that does the following:
Prompts the user for a string (see the get_string function from simpleio).
Determines if the given string is a balanced combination of the following six symbols:
( )
{ }
[ ]
Examples of balanced strings:
()
(())
()()
{()()}
[]
[()[]{}]()
Examples of unbalanced strings:
(
{}}
()[}
[()}
)
Program Behavior:
The program takes no command
Write a program called balanced that does the following: Prompts the user for a string (see the get_string function from simpleio). Determines if the given string is a balanced combination of the following six symbols: ( ) { } [ ] Examples of balanced strings: () (()) ()() {()()} [] [()[]{}]() Examples of unbalanced strings: ( {}} ()[} [()} ) Program Behavior: The program takes no command A line arguments. When run it prompts the user for a string and then reports if it is legal or not. It then terminates. Examples: Formal recursive definition of a balanced string: 1. (BASIS) The empty string is balanced (in C, a string of length zero) 2. (NESTING) If S is a balanced string, then: a. (S) is also a balanced string b. {S} is also a balanced string and c. [S] is also a balanced string. 3. (CONCATENATION) If A and B are both balanced strings, then 4. A string is balanced if and only if it can be generated by a sequence of application of the above rules. Extraneous characters: If the given string contains any characters other than the AB is also a balanced string. six characters, it should be automatically considered not balanced / not legal for the purposes of this assignment.Explanation / Answer
#include <stdio.h>
#include <conio.h>
#include <string.h>
void balanced(char str[]);
main()
{
char str[15];
printf("enter a string:");
gets(str);
balanced(str);
}
void balanced(char str[])
{
int po=0,pc=0,co=0,cc=0,so=0,sc=0,i=0;
while(str[i]!='')
{
if(str[i]=='(')
{
po++;
}
else if(str[i]==')')
{
pc++;
}
else if(str[i]=='[')
{
so++;
}
else if(str[i]==']')
{
sc++;
}
else if(str[i]=='{')
{
co++;
}
else if(str[i]=='}')
{
cc++;
}
i++;
}
if((po==pc)&&(so==sc)&&(co==cc))
{
printf("string is Balanced. Good Day!");
}
else
{
printf("string is not Balanced. Still, Good Day! :) ");
}
}
// All the Best. :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.