WAP to print comments in a given program


#include<stdio.h>
#include<conio.h>
void main()
{
  char str[25],ch,words[50][25];
  int i,k,j,w=0;
  FILE *fp;
  clrscr();
  fp=fopen("tcom.c","r");
  printf("\n File: tcom.c\n\n");
  if(fp!=NULL)
  {
    do
    {
      j=0;
      ch='d';
      strcpy(str,"");
      while(ch!=' ')
      {
      ch=getc(fp);
      putchar(ch);
      if(ch==EOF) break;
      str[j]=ch;
      if(ch!=' '&&ch!='\n'&&ch!=';'&&ch!='/'&&ch!='*')
      j++;
      if(ch==' '||ch=='\n'||ch==';'||ch=='/'||ch=='*') break;
      }
      str[j]='\0';

      strcpy(words[w++],str);
      if(ch=='/')
      strcpy(words[w++],"/");

      if(ch=='*')
      strcpy(words[w++],"*");

      if(ch==';')
      strcpy(words[w++],";");

      if(ch=='\n')
      strcpy(words[w++],"newline");

      if(ch==EOF)
      strcpy(words[w++],"EOF");

      if(ch==' ')
      strcpy(words[w++]," ");

      j=0;
    }
    while(ch!=EOF);
  }


  printf("\n\n\n* Single line comments are:\n");
  for(i=0;i<w;i++)
  {//1
    if(!strcmp(words[i],"/"))
    {//2
      ++i;
      /*single line*/
      if(!strcmp(words[++i],"/"))
      {//3
         do
         {//4
             i=i+1;
             if(strcmp(words[i],"newline")&&strcmp(words[i],"EOF"))
             printf("%s",words[i]);
             if(!strcmp(words[i],"newline")||(!strcmp(words[i],"EOF")))
             break;
         }while(i<w);//4
         printf("\n");
      }//3
      /*single line*/


    }//2

  }//1



  printf("\n\n\n* Multiple line comments are:\n");
  for(i=0;i<w;i++)
  {//1
    if(!strcmp(words[i],"/"))
    {//2

      ++i;
      /*multiline*/
      if(!strcmp(words[++i],"*"))
      {//5
       do
       {//6
                  if(strcmp(words[++i],"*"))
                  printf("%s",words[i]);
                  else
                  { //7
                              if(strcmp(words[++i],"/"))
                              {//8
                              printf("\n");
                              break;
                              } //8
                              else
                              printf("%s",words[++i]);
                  }//7
       }while(i<w);//6
      }//5
      /*multi line*/

    }//2

  }//1

  fclose(fp);
  getch();
}

Post a Comment

0 Comments