Skip to content

Instantly share code, notes, and snippets.

@rjwebb
Created May 9, 2017 21:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rjwebb/c3b406938236e1be34889564c106f515 to your computer and use it in GitHub Desktop.
Save rjwebb/c3b406938236e1be34889564c106f515 to your computer and use it in GitHub Desktop.
A little C program (script!) that checks if the braces are closed on stdin.
#include <stdio.h>
int main(void)
{
FILE *fp;
fp = stdin;
int depth = 0;
int c;
while (!feof(fp)) {
c = fgetc(fp);
if (c == '{') {
depth++;
} else if (c == '}') {
depth--;
}
}
if (depth == 0) {
printf("All braces have been closed!\n");
} else if (depth > 1) {
printf("Some braces have been left open! (%c)\n", depth);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment