So I am writing the solution [including nested comments and special cases, since I don't like half-assing]
I wrote a string that has special cases.
Code:
printf("\"test/*iello*/"); /*hello*/ //hello
It is coming out to
Code:
printf("\"test/*iello*/"); /
My code is
Code:
void remcomments( char s[] )
{
printf("Removing comments\n");
int slcflag, mlcflag, qflag;
int readindex, writeindex;
slcflag = mlcflag = readindex = qflag = writeindex = 0;
int loop = 0;
while( s[readindex] != EOF )
{
printf("Loop %d\n", loop);
if( s[readindex] == '\n' && mlcflag == 0 )
{
s[writeindex] = s[readindex];
slcflag = 0;
writeindex++;
}
else
{
if( qflag == 0 )
{
if( s[readindex] == '"' )
{
if( s[readindex - 1] != '\\' )
{
qflag = 1;
}
else if( s[readindex - 1] == '\\' && s[readindex - 2] == '\\' )
{
qflag = 1;
}
}
if( qflag == 0 )
{
if( slcflag == 0 && mlcflag == 1 && s[readindex] == '*' && s[readindex + 1] == '/' )
{
readindex++;
mlcflag = 0;
}
else if( s[readindex] == '/' )
{
if( mlcflag == 0 && s[readindex + 1] == '/' )
{
readindex++;
slcflag = 1;
}
else if( slcflag == 0 && s[readindex + 1] == '*' )
{
readindex++;
mlcflag = 1;
}
}
}
}
else if( qflag == 1 )
{
if( s[readindex] == '"')
{
if( s[readindex - 1] != '\\')
{
qflag = 0;
}
else if( s[readindex - 1] == '\\' && s[readindex - 2] == '\\')
{
qflag = 0;
}
}
}
if( mlcflag == 0 && slcflag == 0 )
{
s[writeindex] = s[readindex];
writeindex++;
}
}
loop++;
readindex++;
printf("end of loop");
}
s[writeindex] = '\0';
}
Where am I going wrong? I can't figure it out.