Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

remove comments

Posted on 2003-03-24
6
Medium Priority
?
348 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
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

579 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