WA on case 2! Here is my code :(
//WA..
#include <stdio.h>
#include <string.h>
#define maxn 100100
#define lcase(a) (a>='A'&&a<='Z'?a+32:a)
char s[maxn],ch;
long sp;
char strs[35][30] = {
"and",
"array",
"begin",
"case",
"class",
"const",
"div",
"do",
"else",
"end",
"for",
"function",
"if",
"implementation",
"interface",
"mod",
"not",
"of",
"or",
"procedure",
"program",
"record",
"repeat",
"shl",
"shr",
"string",
"then",
"to",
"type",
"unit",
"until",
"uses",
"var",
"with",
"while"
};
char tmp[maxn];
char tmp2[maxn];
long bs () {
long l, m, r, t;
l = 0;
r = 34;
while (l<=r) {
m = ((l+r)>>1);
t = strcmp(strs[m],tmp2);
if (t == 0)
return m;
if (t > 0)
r = m - 1;
else
l = m + 1;
}
return -1;
}
void comment (long l, long r) {
printf("<span class=comment>");
for (;l<=r;l++)
printf("%c",s[l]);
printf("</span>");
}
void string (long l, long r) {
printf("<span class=string>");
for (;l<=r;l++)
printf("%c",s[l]);
printf("</span>");
}
void number (long l, long r) {
printf("<span class=number>");
for (;l<=r;l++)
printf("%c",s[l]);
printf("</span>");
}
void identifier (long l, long r) {
long i;
i = 0;
for (;l<=r;l++) {
tmp2[i] = lcase(s[l]);
tmp[i ++] = s[l];
}
tmp2[i] = 0;
tmp[i] = 0;
i = bs();
if (i > -1)
printf("<span class=keyword>%s</span>",tmp);
else
printf("%s",tmp);
}
int main () {
long i,j,t;
sp = 0;
while (scanf("%c",&ch) > 0)
s[sp++] = ch;
s[sp] = 0;
for (i=0;i<sp;) {
if (s[i] == '{') {
//Comment Type 1
j = i + 1;
while (s[j] != '}')
j ++;
comment(i,j);
i = j + 1;
}
else
if (s[i] == '/' && s[i+1] == '/') {
//Comment Type 2
j = i + 1;
while (s[j] != '\n')
j ++;
comment(i,j - 1);
i = j;
}
else
if (s[i] == '\'') {
j = i + 1;
while (s[j] != '\'')
j ++;
string(i,j);
i = j + 1;
}
else
if (s[i] == '#' && s[i+1]>='0' && s[i+1]<='9') {
j = i + 2;
while (s[j] >= '0' && s[j] <= '9')
j ++;
string(i,j - 1);
i = j;
}
else
if (s[i]>='0' && s[i]<='9') {
// while (1);
t = 0;
j = i + 1;
while (s[j] >= '0' && s[j] <= '9' || s[j] == '.' && s[j+1] >= '0' && s[j+1] <= '9' && !t) {
if (s[j] == '.')
t ++;
j ++;
}
number(i,j - 1);
i = j;
}
else
if (s[i] >= 'A' && s[i] <= 'Z' || s[i] >= 'a' && s[i] <= 'z' || s[i] == '_') {
j = i + 1;
while (s[j] >= 'A' && s[j] <= 'Z' || s[j] >= 'a' && s[j] <= 'z' || s[j] >= '0' && s[j] <= '9' || s[j] == '_')
j ++;
identifier(i,j - 1);
i = j;
}
/*
else
if (s[i] == '.' && s[i+1] >= '0' && s[i+1] <= '9') {
j = i + 1;
while (s[j] >= '0' && s[j] <= '9')
j ++;
number(i,j - 1);
i = j;
}
*/
else
//Others
printf("%c",s[i++]);
}
return 0;
}