Write a program to check the proper pairing of braces. Your program should have two variables: one to keep track of left braces, say, left_cnt and the other to keep track of right braces, say right_cnt. Both variables should be initialized to zero. Your program should read and print each character in the input file. The appropriate variable should be incremented each time a brace is encountered. If right_cntever exceeds the value of left_cnt , your program should insert the character pair ?? atthat point in the output. After all the characters in the input file have beenprocessed, the two variables left_cnt and right_cnt should have the same value;if not and left_cnt is larger than right_cnt then a message should be printed that includes the number of right braces missing as a series ofthat many }s for example ERROR : Missing right braces: }}}
#include<stdio.h>
#include<math.h>
int main()
{
int left,right,d;
char ch;
left=0;
right=0;
d=0;
FILE *fp;
fp=fopen("ex.c","r");
if(fp==NULL)
{
printf("\n file unable to create : 1.c");
exit(1);
}
while((ch=fgetc(fp))!=EOF)
{
if(ch=='{')
{
left++;
}
if(ch=='}')
{
right++;
}
if(right>left)
{
printf("error has occured");
exit(1);
}
}
fclose(fp);
if(left==right)
{
printf("left and right are equal ");
}
else
{
d=left-right;
printf("missing right braces = %d",d);
}
}
#include<math.h>
int main()
{
int left,right,d;
char ch;
left=0;
right=0;
d=0;
FILE *fp;
fp=fopen("ex.c","r");
if(fp==NULL)
{
printf("\n file unable to create : 1.c");
exit(1);
}
while((ch=fgetc(fp))!=EOF)
{
if(ch=='{')
{
left++;
}
if(ch=='}')
{
right++;
}
if(right>left)
{
printf("error has occured");
exit(1);
}
}
fclose(fp);
if(left==right)
{
printf("left and right are equal ");
}
else
{
d=left-right;
printf("missing right braces = %d",d);
}
}
Write a program to check the proper pairing of braces. Your program should have two variables: one to keep track of left braces, say, left_cnt and the other to keep track of right braces, say right_cnt. Both variables should be initialized to zero. Your program should read and print each character in the input file. The appropriate variable should be incremented each time a brace is encountered. If right_cntever exceeds the value of left_cnt , your program should insert the character pair ?? atthat point in the output. After all the characters in the input file have beenprocessed, the two variables left_cnt and right_cnt should have the same value;if not and left_cnt is larger than right_cnt then a message should be printed that includes the number of right braces missing as a series ofthat many }s for example ERROR : Missing right braces: }}}
Reviewed by Unknown
on
03:28
Rating:
No comments: