Unreasonably complete source code:
#include <stdio.h>
main()
{
int c;
int x;
int nl_counter = 1;
int parens, brackets, braces, dubQuotes, singQuotes, comments;
int parenLines[99], bracketLines[99], braceLines[99], dubQuoteLines[99], singQuoteLines[99], commentLines[99];
int inParen, inBracket, inBrace, inDubQuote, inSingQuote, inComment;
comments = parens = brackets = braces = dubQuotes = singQuotes = inComment = inParen = inBracket = inBrace = inDubQuote = inSingQuote = 0;
for(x = 0; x < 99; x++)
parenLines[x] = bracketLines[x] = braceLines[x] = dubQuoteLines[x] = singQuoteLines[x] = commentLines[x] = 0;
while((c = getc(stdin)) != EOF)
{
if(c == '\n')
nl_counter++;
if(c == '/' && inComment == 0)
{
c = getc(stdin);
if(c == '*')
{
inComment = 1;
commentLines[comments] = nl_counter;
comments++;
}
else if(c == '\n')
nl_counter++;
}
if(inComment == 1)
{
if(c == '*')
{
c = getc(stdin);
if(c == '/')
{
inComment = 0;
comments--;
}
else if(c == '\n')
nl_counter++;
}
}
if(c == '(' && inComment == 0)
{
inParen = 1;
parenLines[parens] = nl_counter;
parens++;
}
if(inParen == 1 && inComment == 0)
{
if(c == ')')
{
inParen = 0;
parens--;
}
}
if(c == '[' && inComment == 0)
{
inBracket = 1;
bracketLines[brackets] = nl_counter;
brackets++;
}
if(inBracket == 1 && inComment == 0)
{
if(c == ']')
{
inBracket = 0;
brackets--;
}
}
if(c == '{' && inComment == 0)
{
inBrace = 1;
braceLines[braces] = nl_counter;
braces++;
}
if(inBrace == 1 && inComment == 0)
{
if(c == '}')
{
inBrace = 0;
braces--;
}
}
if(c == '\"' && inComment == 0 && inDubQuote == 0)
{
inDubQuote = 1;
dubQuoteLines[dubQuotes] = nl_counter;
dubQuotes++;
}
else if(inDubQuote == 1 && inComment == 0)
{
if(c == '\"')
{
inDubQuote = 0;
dubQuotes--;
}
}
if(c == '\'' && inComment == 0 && inSingQuote == 0)
{
inSingQuote = 1;
singQuoteLines[singQuotes] = nl_counter;
singQuotes++;
}
else if(inSingQuote == 1 && inComment == 0)
{
if(c == '\'')
{
inSingQuote = 0;
singQuotes--;
}
}
}
if(parens > 0)
{
for(x = 0; x < parens; x++)
printf("unmatched parenthesis on line %d\n", parenLines[x]);
printf("\n");
}
if(brackets > 0)
{
for(x = 0; x < brackets; x++)
printf("unmatched bracket on line %d\n", bracketLines[x]);
printf("\n");
}
if(braces > 0)
{
for(x = 0; x < braces; x++)
printf("unmatched brace on line %d\n", braceLines[x]);
printf("\n");
}
if(dubQuotes > 0)
{
for(x = 0; x < dubQuotes; x++)
printf("unmatched double quote on line %d\n", dubQuoteLines[x]);
printf("\n");
}
if(singQuotes > 0)
{
for(x = 0; x < singQuotes; x++)
printf("unmatched single quote on line %d\n", singQuoteLines[x]);
printf("\n");
}
if(comments > 0)
{
for(x = 0; x < comments; x++)
printf("unmatched comment on line %d\n", commentLines[x]);
printf("\n");
}
getc(stdin);
getc(stdin);
}
Less redundant source code (to the point):
#include <stdio.h>
main()
{
int c;
int x;
int nl_counter = 1;
int dubQuotes, singQuotes;
int dubQuoteLines[99], singQuoteLines[99];
int inDubQuote, inSingQuote;
dubQuotes = singQuotes = inDubQuote = inSingQuote = 0;
for(x = 0; x > 99; x++)
dubQuoteLines[x] = singQuoteLines[x] = 0;
while((c = getc(stdin)) != EOF)
{
if(c == '\n')
nl_counter++;
if(c == '\"' && inDubQuote == 0)
{
inDubQuote = 1;
dubQuoteLines[dubQuotes] = nl_counter;
dubQuotes++;
}
else if(inDubQuote == 1)
{
if(c == '\"')
{
inDubQuote = 0;
dubQuotes--;
}
}
if(c == '\'' && inSingQuote == 0)
{
inSingQuote = 1;
singQuoteLines[singQuotes] = nl_counter;
singQuotes++;
}
else if(inSingQuote == 1)
{
if(c == '\'')
{
inSingQuote = 0;
singQuotes--;
}
}
}
if(dubQuotes > 0)
{
for(x = 0; x < dubQuotes; x++)
printf("unmatched double quote on line %d\n", dubQuoteLines[x]);
printf("\n");
}
if(singQuotes > 0)
{
for(x = 0; x < singQuotes; x++)
printf("unmatched single quote on line %d\n", singQuoteLines[x]);
printf("\n");
}
getc(stdin);
getc(stdin);
}
I haven’t managed to write for escape sequences yet, but still, I don’t understand why all the brackets and curly braces work, but if I put "it and then after pressing enter and ^ z (EOF), it does not display my message “Unrivaled double quote in line 1 ". I compensated for the fact that they have the same character with if-else and extra && inSingQuote == 0/ && inDubQuote == 0in the initial check of the initial quote. I just do not see any holes in the logic.