Thread: Writing a program to remove comments. Not working

  1. #16
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by Maz
    For (compatible) types, assignment would work. For types, comparison would work. And sizeof would work too - unless type is incomplete.

    I guess you can see the fundamental difference between character arrays and C-strings?
    Nope. Because none of the things you said seem to work for arrays either. Well, sizeof does, depending on the locality of the array, but that is also true for C strings.

  2. #17
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Also I would be thankful if difference between:
    prints the single char in array s, at index i
    Code:
        printf ("%c",s[i]);
    and
    prints the string (may be multiple chars), in array s, beginning at index i
    Code:
        printf ("%s",s[i]);
    and
    prints the string in array s, beginning at the first char in s
    Code:
        printf ("%s",s);
    and
    prints the single char in array s, at location s[0]. The name of the array will constantly have the address of the first element of it's array.
    Code:
        printf ("%c",s);
    can be explained.

    Alter ego, if you're still around, let us know where you are in your program, now.

  3. #18
    Registered User Maz's Avatar
    Join Date
    Nov 2005
    Location
    Finland
    Posts
    194
    Quote Originally Posted by whiteflags View Post
    Nope. Because none of the things you said seem to work for arrays either. Well, sizeof does, depending on the locality of the array, but that is also true for C strings.
    Correct. assignment / comparison wont work for arrays either.

    Mmm.. What do you mean by saying that sizeof() works for C strings?

    Code:
    char str[20]="foobar";
    there str is an array, but not string. (string cannot contain NULL byte other but at the end - bar does contain it). sizeof(bar) will result 20 on any system I own.

    Code:
    char *str2="foobar";
    there str2 is a C string (literal). It fullfills requirements for C string. How ever, sizeof(str2) will give the size of char * (pointer) type.


    Hmm... Now you got me actually interested in what C standard does say. I found following:

    ISO/IEC JTC1/SC22/WG14 N794

    (correct me if I am looking for wrong document).

    Now, out of the curiosity (yeah, I'm not a cat), why array assignment does not work?

    If we assumed an array would specify a type, then:

    Two types have compatible type if their types are the
    same....




    I even tested the compilation error gcc 4.3.2 gives:

    if we have:

    Code:
    char bar[25];
    char baz[25];
    
    bar=baz;
    =>
    error: incompatible types in assignment?

    (Well, I am by no means accusing gcc being wrong - I know it must be my ability to digest that text).

    I would appreciate it if someone told me what do I miss here.

  4. #19
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Maz View Post
    Mmm.. What do you mean by saying that sizeof() works for C strings?
    Code:
    printf( "sizeof( \"hello world\" ) is %d\n", sizeof( "hello world" ) );

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #20
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Maz
    What do you mean by saying that sizeof() works for C strings?
    Probably something like: sizeof("Maz"). Technically though, the operand is a (const) char[4].

    Quote Originally Posted by Maz
    I even tested the compilation error gcc 4.3.2 gives:
    (...)
    error: incompatible types in assignment?
    Probably just a misleading error message, i.e., it is really trying to say that the types are incompatible for use in an assignment.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #21
    Registered User
    Join Date
    Apr 2011
    Posts
    55
    Thanks everyone. I was down with fever hence couldn't check the thread. Some engrossing discussions here . Big thanks for clarifying on types of data.

    However I am still working on the program & have modified it a bit. My approach is to check for '/' & '*' if found true; fill the array with blanks & exit. Finally print everything that's up there in the array. However I am unable to pin-point my mistake because program is only accepting input (no output).

    Code:
    #include <stdio.h>
    
    int main(void)
    {
    int c =0;
    int i =0;
    int s[1000];
    int in_quotes = 0;
    
    
    for (i=0; i < 999 && (c=getchar())!=EOF; ++i)
    {
    
    	s[i] = c;
    
    if (s[i] == '/' && s[i+1] == '*') /*entering the comment*/
    {
    
             i = i+2;	 
             in_quotes = 1;
    	 s[i] = '/b';       /*filling blanks*/
             
    
    if (s[i] == '*' && s[i+1] == '/') /* now exiting
    {
    
              i = i+2;          
              in_quotes = 0;
    
    }
    
    }
    if (in_quotes == 1)
    
    {
    	printf ("%s",s[i]);
    }
    }
    
    
    }
    Last edited by alter.ego; 09-29-2011 at 01:56 AM.

  7. #22
    Registered User Maz's Avatar
    Join Date
    Nov 2005
    Location
    Finland
    Posts
    194
    Code:
    /* You're evaluating s[i+1] here... When will s[i+1] be set? */
    if (s[i] == '/' && s[i+1] == '*') /*entering the comment*/

  8. #23
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The preferred way to work with a string of text is:

    1) put the whole string (if not too huge), into a char array1

    2) make all your tests and modifications that you need to do, on the array1 chars.

    3) when finished with all your tests and changes, either compact array1, or copy the good parts you need, and put them into array2, or use sscanf() to take what you want from array1, and assign it to other variables. sscanf() is just like scanf(), but you need to first, add the name of the string that you want it to scan. Same format, otherwise.

    In your program, you're assigning the char c into the array, before you make any tests, which seems awkward to work with, logically.

    Glad you're feeling better, Alter ego.

  9. #24
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by alter.ego View Post
    However I am unable to pin-point my mistake because program is only accepting input (no output).
    Well if you're on windows, you should use CTRL Z to send EOF to your program. If not windows, then CTRL D might work. It will stop for sure, because getchar() stops reading after seeing EOF. Now what's after that loop? Your output code.

  10. #25
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Some posts in this thread were moved to Types in C.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #26
    Registered User
    Join Date
    Apr 2011
    Posts
    55
    Thanks for helping me out; I am loving this.

    Quote Originally Posted by Adak View Post
    In your program, you're assigning the char c into the array, before you make any tests, which seems awkward to work with, logically.
    In fact that is wrong & I figured that yesterday. It truly stands out for all wrong reasons. Glad you noticed that. I am still working on the code intermittently; will find a way to get it right out (now that you have given me some pointers).

    Yes, I am feeling better, thanks for asking .

  12. #27
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    What are your logical rules for the program? Have you specified them?

    e.g.:
    1)
    In any line of text, if two slashes ( // ) are next to each other, the rest of the line of text, is a comment.

    2)
    In any line of text, a slash and adjacent asterisk /* begin a comment. All text after that, until a */ is reached, is a comment.

    and of course:
    3)
    All comments starting char, is overwritten with a \n (newline, and an end of string char), \0. Subsequent lines of comment text, including the newline and end of string char, are not copied into the new (non commented), output.

    So, what's working, and what's not? < slacker >

  13. #28
    Registered User
    Join Date
    Apr 2011
    Posts
    55
    Initially I am keeping it simple. I amonly checking comments between /* and */. If found true, count the caharcters between them & replace with blanks. That's my simple logic. Beats me , why it is not working?

    Code:
    #include <stdio.h>
    
    int main(void)
    {
    int c =0;
    int i =0;
    int m =0;
    int s[1000];
    int in_quotes = 0;
    
    
    for (i=0; i < 999 && (c=getchar())!= EOF; ++i)
    {
    
    	s[i] = c;
    
    	 if (in_quotes == 0)
    
    	{
    	if (s[i] == '/' && s[i+1] == '*')
    {
    
    		i = i+2;
    		in_quotes = 1;
    }
    
    	if (s[i] == '*' && s[i+1] == '/')
    			{
    				i =i+2;
    				in_quotes = 0;
    			}
    	if (in_quotes == 1)
    	{
    	i++;
    
    
    	for (m = 0;m <=i; m++)
    	{
    
    		s[m] = ' ';
    		}
    	}
    	printf ("%s", s);
    }
    	 else
    	 {
    		 printf ("%c", s[i]);
    	 }
    }
    
    }
    Last edited by alter.ego; 10-01-2011 at 12:37 AM.

  14. #29
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    @alter.ego ...

    What I see is that you are very busy trying to code the solution to a problem you do not fully understand... Give yourself the problem... Sit thee down with no computer and two pieces of paper. On one page is a few lines of source code you have to decomment the other page is blank... according to the task you have to copy the source code to the second piece of paper but without the comments...

    Go ahead, ACTUALLY do this... but make careful note of HOW you do it...
    You might say, I copy the first word... Ok how do you copy a word? Well I copy each letter in turn...
    But how do you know when to and when not to copy a letter?

    Work the problem right down to super-idiot sized steps --because that's what you are dealing with, a super-idiot disguised as a computer-- and write a set of instructions detailing exactly how to copy that source code... Now you know how to solve the problem.

    Next you need to spend just a few minutes of planning... based upon your instructions, you should decide what functions, variables, etc. you are likely to need and rough out the general structure of what you're going to write. For example: if there's something you have to do 2 or more times, it might be a candidate for a function. If you need to do something multiple times, in a row, you might consider a loop...

    Ok now you have a plan... you can begin writing code. Work in small steps break it down into blobs... "user input", "display", "Calculation", "Files", etc get one section working before you move on to the next...

    Then once it's all done... test your work until you are satisfied it works as intended.

    4 steps... Analyse -> Plan -> Write -> Test ... the same for every program you write...

  15. #30
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Study the problem - which is not as easy as it seems. We have years of language classes that, (perhaps despite the assertions of Mrs. Gibbs*), have actually become part of us.

    So it's tough to somehow work on these simple language problems, as if we knew nothing about how language works. But that's what you have to do, to get the tiny steps you need, to tell the computer how to work through this exercise.

    Do it by hand, until you can see these simple patterns. Then write them down in small steps.

    That's the start of your program, in pseudo code. Now add to it, and when it's about right, THEN use it to code up the backbone of your program's algorithm.

    Be careful where the newlines and end of string char's are. You can't see them, but they're a critical part of working with strings and chars.





    *a pseudonym for your language teacher

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 58
    Last Post: 01-31-2011, 02:45 AM
  2. [C] remove comments
    By Tool in forum C Programming
    Replies: 50
    Last Post: 11-29-2009, 04:57 AM
  3. Remove comments
    By St0rM-MaN in forum C Programming
    Replies: 4
    Last Post: 05-18-2007, 11:03 PM
  4. program to remove comments from source
    By Abda92 in forum C Programming
    Replies: 12
    Last Post: 12-25-2006, 05:18 PM
  5. remove comments from source code
    By limbo100 in forum C Programming
    Replies: 2
    Last Post: 09-29-2001, 06:25 PM