K&R2, exercise 1-23

A

arnuld

there are no compile-time errors or warning. it runs but it does not
do what it is supposed to.

WANTED: to print the "input C programme" with all comments removed.

GOT: does not print anything.

-------- PROGRAMME -----------
/* K&R2: exercise 1-23

STATEMENT:
write aprogramme to remove all comments from a C programme
dont forget to handle quoted strings and character constants properly.

METHOD:

1.) this programme gets its input a line at a time. it copies all the
lines (full input) into a characters array.

2.) we then call a function "remove_comments" which removes all the
comments from the input.

3.) we then print the "array" which now does not contain any comments.

*/


#include<stdio.h>

#define MAXLENGTH 30000

int getline(char arr[]);
void remove_comments(char arr[]);

int main()
{
int i = 0;
char arr[MAXLENGTH];

/* initialising array */
for(i = 0; i < MAXLENGTH - 1; ++i)
arr = 0;


while(getline(arr))
;

remove_comments(arr);
printf("-------------------------\n");
printf("%s\n", arr);

return 0;
}


int getline(char arr[])
{
int c = 0;
int i = 0;

for(i = 0; i < MAXLENGTH - 1 && (c = getchar()) != EOF && c != '\n';
++i)
arr = c;

if(c == '\n')
arr[i++] = '\n';

arr = '\0';

return i;
}

void remove_comments(char arr[])
{
int i = 0;
int j = 0;
int incomment = 0;
char arr2[MAXLENGTH -1];

for(i=0; i < MAXLENGTH - 1; ++i)
arr2 = 0;


for(i=0; arr != '\0'; ++i)
{
if(arr != '"')
{
if(arr == '/' && arr[i+1] == '*')
incomment = 1;
}


if(arr == '*' && arr[i+1] == '/' && arr[i+2] != '"')
incomment = 0;

if(incomment == 0)
arr2[j++] = arr;
}

arr2[j] = '\0';

for(i = 0; arr2 != '\0'; ++i)
arr = arr2;
}

----------- OUTPUT ----------------
[arch@voodo kr2]$ gcc -ansi -pedantic -Wall -Wextra -O ex_1-23.c
[arch@voodo kr2]$ ./a.out
like this
 
J

Jonas

arnuld said:
there are no compile-time errors or warning. it runs but it does not
do what it is supposed to.

WANTED: to print the "input C programme" with all comments removed.

GOT: does not print anything.

-------- PROGRAMME -----------
/* K&R2: exercise 1-23

STATEMENT:
write aprogramme to remove all comments from a C programme
dont forget to handle quoted strings and character constants properly.

METHOD:

1.) this programme gets its input a line at a time. it copies all the
lines (full input) into a characters array.

2.) we then call a function "remove_comments" which removes all the
comments from the input.

3.) we then print the "array" which now does not contain any comments.

*/


#include<stdio.h>

#define MAXLENGTH 30000

int getline(char arr[]);
void remove_comments(char arr[]);

int main()
{
int i = 0;
char arr[MAXLENGTH];

/* initialising array */
for(i = 0; i < MAXLENGTH - 1; ++i)
arr = 0;


while(getline(arr))
;


Try inserting

printf("%s\n", arr);

here for debug purposes...

remove_comments(arr);
printf("-------------------------\n");
printf("%s\n", arr);

return 0;
}


int getline(char arr[])
{
int c = 0;
int i = 0;

for(i = 0; i < MAXLENGTH - 1 && (c = getchar()) != EOF && c != '\n';
++i)
arr = c;

if(c == '\n')
arr[i++] = '\n';

arr = '\0';

return i;
}

void remove_comments(char arr[])
{
int i = 0;
int j = 0;
int incomment = 0;
char arr2[MAXLENGTH -1];

for(i=0; i < MAXLENGTH - 1; ++i)
arr2 = 0;


for(i=0; arr != '\0'; ++i)
{
if(arr != '"')
{
if(arr == '/' && arr[i+1] == '*')
incomment = 1;
}


if(arr == '*' && arr[i+1] == '/' && arr[i+2] != '"')
incomment = 0;

if(incomment == 0)
arr2[j++] = arr;
}

arr2[j] = '\0';

for(i = 0; arr2 != '\0'; ++i)
arr = arr2;
}


You need to think through the different states that you can be in when
parsing the text. You can be in- or outside a string, and in- or outside a
comment. Inside a string, you can't be inside a comment, and vice-versa. It
might help to write the function in pseudo-code before getting down to
details.
 
A

arnuld

Try inserting

printf("%s\n", arr);

here for debug purposes...

i inserted it there and it prints the "arr" without any trouble.
You need to think through the different states that you can be in when
parsing the text. You can be in- or outside a string, and in- or outside a
comment. Inside a string, you can't be inside a comment, and vice-versa. It
might help to write the function in pseudo-code before getting down to
details.

ok, here is the modified code which again does not work:

-------- PROGRAMME ---------
/* K&R2: exercise 1-23

STATEMENT:
write aprogramme to remove all comments from a C programme
dont forget to handle quoted strings and character constants properly.

METHOD:

1.) this programme gets its input a line at a time. it copies all the
lines (full input) into a characters array.

2.) we then call a function "remove_comments" which removes all the
comments from the input.

3.) we then print the "array" which now does not contain any comments.

*/


#include<stdio.h>

#define MAXLENGTH 30000

int getline(char arr[]);
void remove_comments(char arr[]);

int main()
{
int i = 0;
char arr[MAXLENGTH];

/* initialising array */
for(i = 0; i < MAXLENGTH - 1; ++i)
arr = 0;


while(getline(arr))
printf("%s\n", arr);

remove_comments(arr);
printf("-------------------------\n");
printf("%s\n", arr);

return 0;
}


int getline(char arr[])
{
int c = 0;
int i = 0;

for(i = 0; i < MAXLENGTH - 1 && (c = getchar()) != EOF && c != '\n';
++i)
arr = c;

if(c == '\n')
arr[i++] = '\n';

arr = '\0';

return i;
}

void remove_comments(char arr[])
{
int i = 0;
int j = 0;
int incomment = 0;
int inquote = 0;
char arr2[MAXLENGTH -1];

for(i=0; i < MAXLENGTH - 1; ++i)
arr2 = 0;


for(i=0; arr != '\0'; ++i)
{
if(arr == '"')
{
incomment = 0;

if(inquote == 1)
inquote = 0;
else
inquote = 1;
}

if(inquote == 0)
{
if(arr == '/' && arr[i+1] == '*')
incomment = 1;
else if (arr == '*' && arr[i+1] == '/')
incomment = 0;
}

if(incomment == 0)
arr2[j++] = arr;
}

arr2[j] = '\0';

for(i = 0; arr2 != '\0'; ++i)
arr = arr2;
}

-------- OUTPUT ---------
[arch@voodo kr2]$ gcc -ansi -pedantic -Wall -Wextra -O ex_1-23.c
[arch@voodo kr2]$ ./a.out
like this
like this

and this
and this

-------------------------

[arch@voodo kr2]$ ./a.out
"like this"
"like this"

/* print "this" */
/* print "this" */
 
F

Fred Kleinschmidt

arnuld said:
Try inserting

printf("%s\n", arr);

here for debug purposes...

i inserted it there and it prints the "arr" without any trouble.
You need to think through the different states that you can be in when
parsing the text. You can be in- or outside a string, and in- or outside
a
comment. Inside a string, you can't be inside a comment, and vice-versa.
It
might help to write the function in pseudo-code before getting down to
details.

ok, here is the modified code which again does not work:

-------- PROGRAMME ---------
/* K&R2: exercise 1-23

STATEMENT:
write aprogramme to remove all comments from a C programme
dont forget to handle quoted strings and character constants properly.

METHOD:

1.) this programme gets its input a line at a time. it copies all the
lines (full input) into a characters array.

2.) we then call a function "remove_comments" which removes all the
comments from the input.

3.) we then print the "array" which now does not contain any comments.

*/


#include<stdio.h>

#define MAXLENGTH 30000

int getline(char arr[]);
void remove_comments(char arr[]);

int main()
{
int i = 0;
char arr[MAXLENGTH];

/* initialising array */
for(i = 0; i < MAXLENGTH - 1; ++i)
arr = 0;


while(getline(arr))
printf("%s\n", arr);

remove_comments(arr);
printf("-------------------------\n");
printf("%s\n", arr);

return 0;
}


int getline(char arr[])
{
int c = 0;
int i = 0;

for(i = 0; i < MAXLENGTH - 1 && (c = getchar()) != EOF && c != '\n';
++i)
arr = c;

if(c == '\n')
arr[i++] = '\n';

arr = '\0';

return i;
}

void remove_comments(char arr[])
{
int i = 0;
int j = 0;
int incomment = 0;
int inquote = 0;
char arr2[MAXLENGTH -1];

for(i=0; i < MAXLENGTH - 1; ++i)
arr2 = 0;


for(i=0; arr != '\0'; ++i)
{
if(arr == '"')
{
incomment = 0;

if(inquote == 1)
inquote = 0;
else
inquote = 1;
}

if(inquote == 0)
{
if(arr == '/' && arr[i+1] == '*')
incomment = 1;
else if (arr == '*' && arr[i+1] == '/')
incomment = 0;
}

if(incomment == 0)
arr2[j++] = arr;
}

arr2[j] = '\0';

for(i = 0; arr2 != '\0'; ++i)
arr = arr2;
}

-------- OUTPUT ---------
[arch@voodo kr2]$ gcc -ansi -pedantic -Wall -Wextra -O ex_1-23.c
[arch@voodo kr2]$ ./a.out
like this
like this

and this
and this

-------------------------

[arch@voodo kr2]$ ./a.out
"like this"
"like this"

/* print "this" */
/* print "this" */


You might want to try writing a very simple starter program
that removes comments assuming there are no quoted strings.
When you get that to work, modify it to handle "/*" and "*/ "that
are inside strings.

Also you will have to worry about things like this:
char *ptr = "This string has an embedded \" ";
 
T

Thad Smith

arnuld said:
there are no compile-time errors or warning. it runs but it does not
do what it is supposed to.

WANTED: to print the "input C programme" with all comments removed.

GOT: does not print anything.

-------- PROGRAMME -----------
/* K&R2: exercise 1-23

STATEMENT:
write aprogramme to remove all comments from a C programme
dont forget to handle quoted strings and character constants properly.

METHOD:

1.) this programme gets its input a line at a time. it copies all the
lines (full input) into a characters array.

2.) we then call a function "remove_comments" which removes all the
comments from the input.

3.) we then print the "array" which now does not contain any comments.

*/


#include<stdio.h>

#define MAXLENGTH 30000

int getline(char arr[]);
void remove_comments(char arr[]);

int main()
{
int i = 0;
char arr[MAXLENGTH];

/* initialising array */
for(i = 0; i < MAXLENGTH - 1; ++i)
arr = 0;


while(getline(arr))
;

remove_comments(arr);
printf("-------------------------\n");
printf("%s\n", arr);

return 0;
}


int getline(char arr[])
{
int c = 0;
int i = 0;

for(i = 0; i < MAXLENGTH - 1 && (c = getchar()) != EOF && c != '\n';
++i)
arr = c;

if(c == '\n')
arr[i++] = '\n';

arr = '\0';

return i;
}

void remove_comments(char arr[])
{
int i = 0;
int j = 0;
int incomment = 0;
char arr2[MAXLENGTH -1];

for(i=0; i < MAXLENGTH - 1; ++i)
arr2 = 0;


for(i=0; arr != '\0'; ++i)
{
if(arr != '"')
{
if(arr == '/' && arr[i+1] == '*')
incomment = 1;
}


if(arr == '*' && arr[i+1] == '/' && arr[i+2] != '"')
incomment = 0;

if(incomment == 0)
arr2[j++] = arr;
}

arr2[j] = '\0';

for(i = 0; arr2 != '\0'; ++i)
arr = arr2;
}


Assuming the first line is 10 characters long, where are you putting the
second line of input?
 
A

arnuld

You might want to try writing a very simple starter program
that removes comments assuming there are no quoted strings.
When you get that to work, modify it to handle "/*" and "*/ "that
are inside strings.

OK, 1st a programme which does not take "quotes" into account. here
it is, it runs fine. i even gave it a C programme as input and it
removed the comments:

---------- PROGRAMME ----------
/* a programme that removes comments which are outside of quotes */

#include<stdio.h>

#define MAXLENGTH 10000

void remove_comments(char arr[]);

int main()
{
int c = 0;
int i =0;
char collect_input[MAXLENGTH];

while((c = getchar()) != EOF)
collect_input[i++] = c;

collect_input = '\0';

remove_comments(collect_input);
printf("----------------------\n%s\n", collect_input);

return 0;
}


void remove_comments(char arr[])
{
int i = 0;
int j = 0;
int incomment = 0;
char arr2[MAXLENGTH];

for(i = 0; arr != '\0'; ++i)
{
if(arr == '/' && arr[i+1] == '*')
incomment = 1;

else if(arr == '*' && arr[i+1] == '/')
incomment = 0;

if(incomment == 0 && arr == '*')
{
i = i + 2;
arr2[j++] = arr;
}

else if(incomment == 0)
arr2[j++] = arr;
}

arr2[j] = '\0';

i = 0;
while((arr = arr2[i++]) != '\0')
;
}

--------- OUTPUT ----------
[arch@voodo kr2]$ ./a.out
like this
and this is a /* comment */ not
----------------------
like this
and this is a not

[arch@voodo kr2]$
Also you will have to worry about things like this:
char *ptr = "This string has an embedded \" ";

one more "if" statement, i guess, but 1st i need to modify my new
programme to handle quoted strings.
 
A

arnuld

You might want to try writing a very simple starter program
that removes comments assuming there are no quoted strings.
When you get that to work, modify it to handle "/*" and "*/ "that
are inside strings.

i have modified my programme to remove quotes from C programme. it
TAKES CARE of quotes without any trouble. look down here for code and
output.
Also you will have to worry about things like this:
char *ptr = "This string has an embedded \" ";


Hmm... this is the only POINT where that i am not able to think of
"How to check for this". i will give it a shot, after taking some
rest.

------ PROGRAMME ---------
/* a programme that removes comments which are outside of quotes */

#include<stdio.h>

#define MAXLENGTH 10000

void remove_comments(char arr[]);

int main()
{
int c = 0;
int i =0;
char collect_input[MAXLENGTH];

while((c = getchar()) != EOF)
collect_input[i++] = c;

collect_input = '\0';

remove_comments(collect_input);
printf("----------------------\n%s\n", collect_input);

return 0;
}


void remove_comments(char arr[])
{
int i = 0;
int j = 0;
int incomment = 0;
int inquote = 0;
char arr2[MAXLENGTH];

for(i = 0; arr != '\0'; ++i)
{
if(arr == '"')
{
if(inquote == 1)
inquote = 0;
else
{
inquote = 1;
incomment = 0;
}
}

else if(arr == '/' && arr[i+1] == '*' && inquote == 0)
incomment = 1;

else if(arr == '*' && arr[i+1] == '/' && inquote == 0)
incomment = 0;


if(incomment == 0 && inquote == 0 && arr == '*')
{
i = i + 2;
arr2[j++] = arr;
}
else if(inquote == 0 && arr == '"')
arr2[j++] = arr;
else if(incomment == 0)
arr2[j++] = arr;
}

arr2[j] = '\0';

i = 0;
while((arr = arr2[i++]) != '\0')
;
}

--------- OUTPUT ---------
[arch@voodo kr2]$ gcc -ansi -Wall -Wextra -pedantic -O new.c
[arch@voodo kr2]$ ./a.out
like "quote"
 
A

arnuld

You might want to try writing a very simple starter program
that removes comments assuming there are no quoted strings.
When you get that to work, modify it to handle "/*" and "*/ "that
are inside strings.

Also you will have to worry about things like this:
char *ptr = "This string has an embedded \" ";

OK, after 2 hours of gruelling work, i did that :)

-------- PROGRAMME ----------
/* a programme that removes comments which are outside of quotes */

#include<stdio.h>

#define MAXLENGTH 10000

void remove_comments(char arr[]);

int main()
{
int c = 0;
int i =0;
char collect_input[MAXLENGTH];

while((c = getchar()) != EOF)
collect_input[i++] = c;

collect_input = '\0';

remove_comments(collect_input);
printf("----------------------\n%s\n", collect_input);

return 0;
}


void remove_comments(char arr[])
{
int i = 0;
int j = 0;
int incomment = 0;
int inquote = 0;
char arr2[MAXLENGTH];

for(i = 0; arr != '\0'; ++i)
{
if(arr == '"')
{
if(arr[i - 1] == '\\')
inquote = 1;
else if(inquote == 1)
inquote = 0;
else
{
inquote = 1;
incomment = 0;
}
}


else if(arr == '/' && arr[i+1] == '*' && inquote == 0)
incomment = 1;

else if(arr == '*' && arr[i+1] == '/' && inquote == 0)
incomment = 0;


if(incomment == 0 && inquote == 0 && arr == '*')
{
i = i + 2;
arr2[j++] = arr;
}
else if(inquote == 0 && arr == '"')
arr2[j++] = arr;
else if(incomment == 0)
arr2[j++] = arr;
}

arr2[j] = '\0';

i = 0;
while((arr = arr2[i++]) != '\0')
;
}

--------- OUTPUT -----------
[arch@voodo kr2]$ gcc -ansi -Wall -Wextra -pedantic -O new.c
[arch@voodo kr2]$ ./a.out
like "this" /* comment */ and
----------------------
like "this" and

[arch@voodo kr2]$ ./a.out
like "/*" done
----------------------
like "/*" done

[arch@voodo kr2]$ ./a.out
like "\" /*" and /* comment */ done
----------------------
like "\" /*" and done

[arch@voodo kr2]$ ./a.out
like "\" and /* inside quote comment */" what about /* this */ done
"*/" do
 
B

Barry Schwarz

ok, here is the modified code which again does not work:
-------- PROGRAMME ---------
/* K&R2: exercise 1-23

STATEMENT:
write aprogramme to remove all comments from a C programme
dont forget to handle quoted strings and character constants properly.

METHOD:

1.) this programme gets its input a line at a time. it copies all the
lines (full input) into a characters array.

2.) we then call a function "remove_comments" which removes all the
comments from the input.

3.) we then print the "array" which now does not contain any comments.

*/


#include<stdio.h>

#define MAXLENGTH 30000

int getline(char arr[]);
void remove_comments(char arr[]);

int main()
{
int i = 0;
char arr[MAXLENGTH];

/* initialising array */
for(i = 0; i < MAXLENGTH - 1; ++i)
arr = 0;


Is there some reason you chose to leave the last element of the array
uninitialized? (You have an unhealthy obsession with MAXLENGTH-1.)
while(getline(arr))
printf("%s\n", arr);

Don't you think you should process the data in arr before you loop
back?
remove_comments(arr);
printf("-------------------------\n");
printf("%s\n", arr);

return 0;
}


int getline(char arr[])
{
int c = 0;
int i = 0;

for(i = 0; i < MAXLENGTH - 1 && (c = getchar()) != EOF && c != '\n';
++i)
arr = c;

if(c == '\n')
arr[i++] = '\n';


Why are you putting a \n at the end of your string?
arr = '\0';


What happens if the user enter MAXLENGTH characters?
return i;
}

void remove_comments(char arr[])
{
int i = 0;
int j = 0;
int incomment = 0;
int inquote = 0;
char arr2[MAXLENGTH -1];

for(i=0; i < MAXLENGTH - 1; ++i)
arr2 = 0;


for(i=0; arr != '\0'; ++i)
{
if(arr == '"')


What happens if the quote is inside a comment?
{
incomment = 0;

if(inquote == 1)
inquote = 0;
else
inquote = 1;
}

if(inquote == 0)
{
if(arr == '/' && arr[i+1] == '*')
incomment = 1;
else if (arr == '*' && arr[i+1] == '/')
incomment = 0;


At this point you also need to skip over the */ or ...
}

if(incomment == 0)
arr2[j++] = arr;


This will copy the two characters that terminate the comment into the
output.
}

arr2[j] = '\0';

for(i = 0; arr2 != '\0'; ++i)
arr = arr2;


The output string is probably shorter than the input string, it would
be a good idea to terminate it.

You really should invest in a debugger if you are not willing to desk
check your code.


Remove del for email
 
B

Barry Schwarz

OK, 1st a programme which does not take "quotes" into account. here
it is, it runs fine. i even gave it a C programme as input and it

Only for a strange definition of fine.
removed the comments:

---------- PROGRAMME ----------
/* a programme that removes comments which are outside of quotes */

#include<stdio.h>

#define MAXLENGTH 10000

void remove_comments(char arr[]);

int main()
{
int c = 0;
int i =0;
char collect_input[MAXLENGTH];

while((c = getchar()) != EOF)

Most terminal input is terminated with ENTER.
collect_input[i++] = c;

collect_input = '\0';

remove_comments(collect_input);
printf("----------------------\n%s\n", collect_input);

return 0;
}


void remove_comments(char arr[])
{
int i = 0;
int j = 0;
int incomment = 0;
char arr2[MAXLENGTH];

for(i = 0; arr != '\0'; ++i)
{
if(arr == '/' && arr[i+1] == '*')
incomment = 1;

else if(arr == '*' && arr[i+1] == '/')
incomment = 0;

if(incomment == 0 && arr == '*')


What will happen to the input statement
x = a*b;
{
i = i + 2;
arr2[j++] = arr;
}

else if(incomment == 0)
arr2[j++] = arr;
}

arr2[j] = '\0';

i = 0;
while((arr = arr2[i++]) != '\0')
;
}

--------- OUTPUT ----------
[arch@voodo kr2]$ ./a.out
like this
and this is a /* comment */ not
----------------------
like this
and this is a not

[arch@voodo kr2]$
Also you will have to worry about things like this:
char *ptr = "This string has an embedded \" ";

one more "if" statement, i guess, but 1st i need to modify my new
programme to handle quoted strings.



Remove del for email
 
B

Barry Schwarz

i have modified my programme to remove quotes from C programme. it
TAKES CARE of quotes without any trouble. look down here for code and
output.

------ PROGRAMME ---------
/* a programme that removes comments which are outside of quotes */

#include<stdio.h>

#define MAXLENGTH 10000

void remove_comments(char arr[]);

int main()
{
int c = 0;
int i =0;
char collect_input[MAXLENGTH];

while((c = getchar()) != EOF)
collect_input[i++] = c;

collect_input = '\0';

remove_comments(collect_input);
printf("----------------------\n%s\n", collect_input);

return 0;
}


void remove_comments(char arr[])
{
int i = 0;
int j = 0;
int incomment = 0;
int inquote = 0;
char arr2[MAXLENGTH];

for(i = 0; arr != '\0'; ++i)
{
if(arr == '"')
{
if(inquote == 1)
inquote = 0;
else
{
inquote = 1;
incomment = 0;
}
}

else if(arr == '/' && arr[i+1] == '*' && inquote == 0)
incomment = 1;

else if(arr == '*' && arr[i+1] == '/' && inquote == 0)
incomment = 0;


if(incomment == 0 && inquote == 0 && arr == '*')
{
i = i + 2;
arr2[j++] = arr;
}


What will this do to the statement
x = a*b;
else if(inquote == 0 && arr == '"')
arr2[j++] = arr;
else if(incomment == 0)
arr2[j++] = arr;
}

arr2[j] = '\0';

i = 0;
while((arr = arr2[i++]) != '\0')


This invokes undefined behavior.
;
}

--------- OUTPUT ---------
[arch@voodo kr2]$ gcc -ansi -Wall -Wextra -pedantic -O new.c
[arch@voodo kr2]$ ./a.out
like "quote"
----------------------
like "quote"

[arch@voodo kr2]$ ./a.out
like "quote" /* comment */ out of comment
----------------------
like "quote" out of comment

[arch@voodo kr2]$ ./a.out
like "/* inside quote */" and /*outsaide quote */ DONE


Remove del for email
 
B

Barry Schwarz

OK, after 2 hours of gruelling work, i did that :)

-------- PROGRAMME ----------
/* a programme that removes comments which are outside of quotes */

#include<stdio.h>

#define MAXLENGTH 10000

void remove_comments(char arr[]);

int main()
{
int c = 0;
int i =0;
char collect_input[MAXLENGTH];

while((c = getchar()) != EOF)
collect_input[i++] = c;

collect_input = '\0';

remove_comments(collect_input);
printf("----------------------\n%s\n", collect_input);

return 0;
}


void remove_comments(char arr[])
{
int i = 0;
int j = 0;
int incomment = 0;
int inquote = 0;
char arr2[MAXLENGTH];

for(i = 0; arr != '\0'; ++i)
{
if(arr == '"')
{
if(arr[i - 1] == '\\')


At this point, you don't know if arr[i-1] exists?
inquote = 1;
else if(inquote == 1)
inquote = 0;
else
{
inquote = 1;
incomment = 0;

What will this do to the statement
x = a+b; /* compute "total" */
}
}


else if(arr == '/' && arr[i+1] == '*' && inquote == 0)
incomment = 1;

else if(arr == '*' && arr[i+1] == '/' && inquote == 0)
incomment = 0;


if(incomment == 0 && inquote == 0 && arr == '*')
{
i = i + 2;
arr2[j++] = arr;
}


What will this do to the statement
x = a*b;
else if(inquote == 0 && arr == '"')
arr2[j++] = arr;
else if(incomment == 0)
arr2[j++] = arr;
}

arr2[j] = '\0';

i = 0;
while((arr = arr2[i++]) != '\0')


This invokes undefined behavior.

You ought to let the dust settle on one program (at least a day or
two) until so you can fix all its problems before running off to do
another. The last three in this thread have several problems in
common.


Remove del for email
 
J

Jonas

arnuld said:
On Apr 6, 11:40 pm, "Fred Kleinschmidt" <[email protected]>
wrote:

You might want to try writing a very simple starter program
that removes comments assuming there are no quoted strings.
When you get that to work, modify it to handle "/*" and "*/ "that
are inside strings.

Also you will have to worry about things like this:
char *ptr = "This string has an embedded \" ";

OK, after 2 hours of gruelling work, i did that :)

-------- PROGRAMME ----------
/* a programme that removes comments which are outside of quotes */

#include<stdio.h>

#define MAXLENGTH 10000

void remove_comments(char arr[]);

int main()
{
int c = 0;
int i =0;
char collect_input[MAXLENGTH];

while((c = getchar()) != EOF)
collect_input[i++] = c;

collect_input = '\0';

remove_comments(collect_input);
printf("----------------------\n%s\n", collect_input);

return 0;
}


void remove_comments(char arr[])
{
int i = 0;
int j = 0;
int incomment = 0;
int inquote = 0;
char arr2[MAXLENGTH];

for(i = 0; arr != '\0'; ++i)
{
if(arr == '"')


If we are inside a comment, we don't care if we're entering a quote or not.
/* "haha */ is just as valid a comment as /* "haha" */ or /* haha */.
{
if(arr[i - 1] == '\\')

If i == 0, you're looking at arr[-1].
inquote = 1;
else if(inquote == 1)
inquote = 0;
else
{
inquote = 1;
incomment = 0;

entering or leaving a quoted string does not affect whether or not we're in
a comment.
}
}


else if(arr == '/' && arr[i+1] == '*' && inquote == 0)
incomment = 1;

else if(arr == '*' && arr[i+1] == '/' && inquote == 0)
incomment = 0;


if(incomment == 0 && inquote == 0 && arr == '*')
{
i = i + 2;
arr2[j++] = arr;
}
else if(inquote == 0 && arr == '"')
arr2[j++] = arr;
else if(incomment == 0)
arr2[j++] = arr;
}

arr2[j] = '\0';

i = 0;
while((arr = arr2[i++]) != '\0')
;
}



The C language aside, it would probably help to look at the algorithm that
the function implements, and separate that from implementation details like
handling arrays in C.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top