Thread: Writing a program to remove comments. Not working

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #23
    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.

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