473,396 Members | 2,034 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 1396

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Chris Readle | last post by:
Ok, I've just recently finished a beginning C class and now I'm working through K&R2 (alongside the C99 standard) to *really* learn C. So anyway, I'm working on an exercise in chapter one which...
16
by: Josh Zenker | last post by:
This is my attempt at exercise 1-10 in K&R2. The code looks sloppy to me. Is there a more elegant way to do this? #include <stdio.h> /* copies input to output, printing */ /* series of...
2
by: arnuld | last post by:
there is a solution on "clc-wiki" for exercise 1.17 of K&R2: http://clc-wiki.net/wiki/K%26R2_solutions:Chapter_1:Exercise_17 i see this uses pointers whereas K&R2 have not discussed pointers...
8
by: arnuld | last post by:
i have created a solutions myself. it compiles without any trouble and runs but it prints some strange characters. i am not able to find where is the trouble. ...
4
by: arnuld | last post by:
as i said, i have restarted the book because i overlooked some material. i want to have some comments/views on this solution. it runs fine, BTW. ------------------ PROGRAMME -------------- /*...
16
by: arnuld | last post by:
i have created solution which compiles and runs without any error/ warning but it does not work. i am not able to understand why. i thought it is good to post my code here for correction before...
19
by: arnuld | last post by:
this programme runs without any error but it does not do what i want it to do: ------------- PROGRAMME -------------- /* K&R2, section 1.6 Arrays; Exercise 1-13. STATEMENT: Write a program...
5
by: arnuld | last post by:
this is a programme that counts the "lengths" of each word and then prints that many of stars(*) on the output . it is a modified form of K&R2 exercise 1-13. the programme runs without any...
16
by: arnuld | last post by:
i am not able to make it work. it compiles without any error but does not work: what i WANTED: 1.) 1st we will take the input in to an array. (calling "getline" in "main") 2.) we will print...
88
by: santosh | last post by:
Hello all, In K&R2 one exercise asks the reader to compute and print the limits for the basic integer types. This is trivial for unsigned types. But is it possible for signed types without...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.