Link to home
Start Free TrialLog in
Avatar of purtutu
purtutu

asked on

remove comments

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);}
}
Avatar of corduroy9
corduroy9


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!



Avatar of purtutu

ASKER

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


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
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
ASKER CERTIFIED SOLUTION
Avatar of gotenks
gotenks

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of purtutu

ASKER

Tahanks very very much for the modified code .It is working properly now