469,613 Members | 1,273 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,613 developers. It's quick & easy.

K&R2, exercise 1-23

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[i] = 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[i] = c;

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

arr[i] = '\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[i] = 0;
for(i=0; arr[i] != '\0'; ++i)
{
if(arr[i] != '"')
{
if(arr[i] == '/' && arr[i+1] == '*')
incomment = 1;
}
if(arr[i] == '*' && arr[i+1] == '/' && arr[i+2] != '"')
incomment = 0;

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

arr2[j] = '\0';

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

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

[arch@voodo kr2]$

Apr 6 '07 #1
12 1224

"arnuld" <ge*********@gmail.comwrote in message
news:11*********************@d57g2000hsg.googlegro ups.com...
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[i] = 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[i] = c;

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

arr[i] = '\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[i] = 0;
for(i=0; arr[i] != '\0'; ++i)
{
if(arr[i] != '"')
{
if(arr[i] == '/' && arr[i+1] == '*')
incomment = 1;
}
if(arr[i] == '*' && arr[i+1] == '/' && arr[i+2] != '"')
incomment = 0;

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

arr2[j] = '\0';

for(i = 0; arr2[i] != '\0'; ++i)
arr[i] = arr2[i];
}
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.

--
Jonas

Apr 6 '07 #2
On Apr 6, 6:31 pm, "Jonas" <spamhereple...@gmail.comwrote:
"arnuld" <geek.arn...@gmail.comwrote in message
while(getline(arr))
;

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[i] = 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[i] = c;

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

arr[i] = '\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[i] = 0;
for(i=0; arr[i] != '\0'; ++i)
{
if(arr[i] == '"')
{
incomment = 0;

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

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

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

arr2[j] = '\0';

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

-------- 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" */

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

[arch@voodo kr2]$

Apr 6 '07 #3

"arnuld" <ge*********@gmail.comwrote in message
news:11**********************@q75g2000hsh.googlegr oups.com...
>On Apr 6, 6:31 pm, "Jonas" <spamhereple...@gmail.comwrote:
"arnuld" <geek.arn...@gmail.comwrote in message

while(getline(arr))
;

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[i] = 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[i] = c;

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

arr[i] = '\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[i] = 0;
for(i=0; arr[i] != '\0'; ++i)
{
if(arr[i] == '"')
{
incomment = 0;

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

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

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

arr2[j] = '\0';

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

-------- 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 \" ";

--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Aero Stability and Controls Computing
Apr 6 '07 #4
arnuld wrote:
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[i] = 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[i] = c;

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

arr[i] = '\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[i] = 0;
for(i=0; arr[i] != '\0'; ++i)
{
if(arr[i] != '"')
{
if(arr[i] == '/' && arr[i+1] == '*')
incomment = 1;
}
if(arr[i] == '*' && arr[i+1] == '/' && arr[i+2] != '"')
incomment = 0;

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

arr2[j] = '\0';

for(i = 0; arr2[i] != '\0'; ++i)
arr[i] = arr2[i];
}
Assuming the first line is 10 characters long, where are you putting the
second line of input?

--
Thad
Apr 7 '07 #5
On Apr 6, 11:40 pm, "Fred Kleinschmidt" <fred.l.kleinmschm...@boeing.comwrote:
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[i] = '\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[i] != '\0'; ++i)
{
if(arr[i] == '/' && arr[i+1] == '*')
incomment = 1;

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

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

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

arr2[j] = '\0';

i = 0;
while((arr[i] = 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.

Apr 7 '07 #6
On Apr 6, 11:40 pm, "Fred Kleinschmidt" <fred.l.kleinmschm...@boeing.comwrote:
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[i] = '\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[i] != '\0'; ++i)
{
if(arr[i] == '"')
{
if(inquote == 1)
inquote = 0;
else
{
inquote = 1;
incomment = 0;
}
}

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

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

arr2[j] = '\0';

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

--------- 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
----------------------
like "/* inside quote */" and DONE

[arch@voodo kr2]$

Apr 7 '07 #7
On Apr 6, 11:40 pm, "Fred Kleinschmidt" <fred.l.kleinmschm...@boeing.comwrote:

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[i] = '\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[i] != '\0'; ++i)
{
if(arr[i] == '"')
{
if(arr[i - 1] == '\\')
inquote = 1;
else if(inquote == 1)
inquote = 0;
else
{
inquote = 1;
incomment = 0;
}
}
else if(arr[i] == '/' && arr[i+1] == '*' && inquote == 0)
incomment = 1;

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

arr2[j] = '\0';

i = 0;
while((arr[i] = 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
----------------------
like "\" and /* inside quote comment */" what about done "*/" do

[arch@voodo kr2]$

Apr 7 '07 #8
On 6 Apr 2007 09:54:03 -0700, "arnuld" <ge*********@gmail.comwrote:
>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[i] = 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[i] = c;

if(c == '\n')
arr[i++] = '\n';
Why are you putting a \n at the end of your string?
>
arr[i] = '\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[i] = 0;
for(i=0; arr[i] != '\0'; ++i)
{
if(arr[i] == '"')
What happens if the quote is inside a comment?
> {
incomment = 0;

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

if(inquote == 0)
{
if(arr[i] == '/' && arr[i+1] == '*')
incomment = 1;
else if (arr[i] == '*' && arr[i+1] == '/')
incomment = 0;
At this point you also need to skip over the */ or ...
> }

if(incomment == 0)
arr2[j++] = arr[i];
This will copy the two characters that terminate the comment into the
output.
}

arr2[j] = '\0';

for(i = 0; arr2[i] != '\0'; ++i)
arr[i] = arr2[i];
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
Apr 7 '07 #9
On 6 Apr 2007 21:35:52 -0700, "arnuld" <ge*********@gmail.comwrote:
>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[i] = '\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[i] != '\0'; ++i)
{
if(arr[i] == '/' && arr[i+1] == '*')
incomment = 1;

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

if(incomment == 0 && arr[i] == '*')
What will happen to the input statement
x = a*b;
> {
i = i + 2;
arr2[j++] = arr[i];
}

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

arr2[j] = '\0';

i = 0;
while((arr[i] = 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
Apr 7 '07 #10
On 6 Apr 2007 22:21:02 -0700, "arnuld" <ge*********@gmail.comwrote:
>
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[i] = '\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[i] != '\0'; ++i)
{
if(arr[i] == '"')
{
if(inquote == 1)
inquote = 0;
else
{
inquote = 1;
incomment = 0;
}
}

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

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

arr2[j] = '\0';

i = 0;
while((arr[i] = 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
----------------------
like "/* inside quote */" and DONE

[arch@voodo kr2]$

Remove del for email
Apr 7 '07 #11
On 6 Apr 2007 22:36:42 -0700, "arnuld" <ge*********@gmail.comwrote:
>
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[i] = '\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[i] != '\0'; ++i)
{
if(arr[i] == '"')
{
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[i] == '/' && arr[i+1] == '*' && inquote == 0)
incomment = 1;

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

arr2[j] = '\0';

i = 0;
while((arr[i] = 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
Apr 7 '07 #12

"arnuld" <ge*********@gmail.comwrote in message
news:11**********************@p77g2000hsh.googlegr oups.com...
>On Apr 6, 11:40 pm, "Fred Kleinschmidt" <fred.l.kleinmschm...@boeing.com>
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[i] = '\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[i] != '\0'; ++i)
{
if(arr[i] == '"')
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[i] == '/' && arr[i+1] == '*' && inquote == 0)
incomment = 1;

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

arr2[j] = '\0';

i = 0;
while((arr[i] = 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.

--
Jonas
Apr 7 '07 #13

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by Chris Readle | last post: by
16 posts views Thread by Josh Zenker | last post: by
2 posts views Thread by arnuld | last post: by
4 posts views Thread by arnuld | last post: by
16 posts views Thread by arnuld | last post: by
19 posts views Thread by arnuld | last post: by
5 posts views Thread by arnuld | last post: by
16 posts views Thread by arnuld | last post: by
88 posts views Thread by santosh | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.