?
Solved

remove comments

Posted on 2003-03-24
6
Medium Priority
?
346 Views
Last Modified: 2010-08-05
Here is the  program.This is to remove comments from a program source code.THe program does not work properly.I can't figure out why?help!!!!
#include <stdio.h>
#include <string.h>
void removeComments(char pro[]){
 int i=0,j=0,len=0,newLen=0;
 char newPro[1000];
 printf("Hi");
 len = strlen(pro);
 printf("length = %d",len);
 printf("%d",len);
 while(i<len){
  if((pro[i] == '/')&&(pro[i+1]=='*')){
   i = i+2;
   while(!((pro[i+1]=='*')&&(pro[i+2]=='/')))
    i++;
   i += 2;
  }
  newPro[j] = pro[i];
  j++;
  i++;
 }
 j=0;
 newLen = strlen(newPro);
 while(j<newLen)
  printf("%s",newPro[j]);
}

void main(){
 void removeComments(char c[]);

 char length,line[50],myPro[1000];
 int k = 0,n=0;
 printf("Enter your program\n");
 scanf("%s",line);
 while(line[0] != '0'){
  length = strlen(line);
  printf("%d",length);
  while(n<length)
   myPro[k++]=line[n++];
  scanf("%s",line);
 }/*end while*/
 for(n=0;n<k;n++){
  printf("%c",myPro[n]);
 removeComments(myPro);}
}
0
Comment
Question by:purtutu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 2

Expert Comment

by:corduroy9
ID: 8198241

Your code seems to get file names, but then it doesn't open each one to interogate the contents.  Instead, it looks like it's trying to remove comments ( /* and not lines that begin with //) from the filename.

Once you get each filename, you should call fopen and then fgets to see if you have comments or not.  This should be done before your while(i<len) loop in the removeComments function.

Good luck!



0
 

Author Comment

by:purtutu
ID: 8199290
Iam not entering file name .Iam manually giving the program
Herre is the output i get.Please help
Enter your program
main(){
7
/*kgkdk*/
9


0
 
LVL 3

Expert Comment

by:prady_21
ID: 8199520
Use strstr to check if the line contains "/*" and "*/".
strstr function returns the pointer of the location where /* is found. So u can copy the contents until /* using strncpy. (see man pages). It is easy
Hope that helps
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:prady_21
ID: 8199774
This code is working fine in my system


#include<stdio.h>
#include<string.h>
/* This program is used for filtering the log files */

int main(void)
{
    char *ptr, *index;
    char array[20][500];
    char line[1000];
    int num,len,i,length;
    num = 0;
    while(gets(line))
    {
       ptr = index = line;
       if(index = strstr(ptr,"/*")) {
          len = index - ptr;
          if ( len > 0 ) {
             strncpy(array[num],ptr,len);
             length=strlen(array[num]);
             array[num][length+1] = '\0';
             num++;
          }
       }
       else {
          strcpy(array[num], ptr);
          num++;
       }
    }
    for(i=0; i<num; i++) {
       printf("%s\n",array[i]);
    }
}


It stores the lines in an array.
Well i havent checked for the end of comment and for other types of comments
You can add your own conditions to it
0
 

Accepted Solution

by:
gotenks earned 120 total points
ID: 8201406
have modified some part of your code... see if this helps.

#include <stdio.h>
#include <string.h>

void removeComments(char * pro){
   int i=0,j=0,len=0,newLen=0;
   char newPro[1000];

   printf("Hi");
   len = strlen(pro);
   printf("\nlength = %d",len);
   while(i < len){
      if ( (pro[i] == '/') && ( i + 1 < len ) && ( pro[i+1] == '*')) {
         i = i + 2;
         while (i < len) {
            if ( (pro[i] == '*') && ( i + 1 < len ) && ( pro[i+1] == '/' )) {
               i = i + 2;
               break;
            } /* end if */
            i++;
         } /* end 2nd while loop */
      } /* end if */
      newPro[j] = pro[i];
      i++;
      j++;
   } /* end 1st while loop */

   /* add a null-terminating character to the end
    * of the string
    */
   newPro[j] = '\0';
   printf("%s\n", newPro);
}

void main(){
   void removeComments(char c[]);
   char length,line[50],myPro[1000];
   int k = 0,n = 0;

   printf("Enter your program\n");
   /* change to gets() cos scanf() will stop
    * scanning right after the blank space
    */
   gets(line);
   while(line[0] != '0'){
      n = 0;
      length = strlen(line);
      while(n<length)
         myPro[k++]=line[n++];

      gets(line);
   } /* end while */

   /* add a null-terminating character to the end
    * of the string
    */
   myPro[k] = '\0';
   for(n = 0; n < k; n++){
      printf("%c",myPro[n]);
   }
   removeComments(myPro);
}

the above code should be able to remove comment that appear in a 'normal' way.
please note that your code still couldn't check those inside the double quotes :
example :
   ...
   printf(" /* not a comment */ ");
   ...

your program will remove the "/* not a comment */" from this statement which is not correct.
and remember to add a '\0' (null-terminating character) to the end of the char array (string).
anyway, hope the above code can get you going... but i haven't fully tested the above code... so no guarantee for bugs free... good luck.

gotenks
0
 

Author Comment

by:purtutu
ID: 8203379
Tahanks very very much for the modified code .It is working properly now
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Suggested Courses
Course of the Month10 days, 1 hour left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question