Showing posts with label Remove. Show all posts
Showing posts with label Remove. Show all posts

How To Write a program to remove all comments from a C program.

Write a program to remove all comments from a C program.


  1. #include <stdio.h> // Includes the standard INPUT/OUTPUT library.
  2. #include "lib/helpers.h" /* Includes our own helper functions. */
  3.  
  4. int main(void)
  5. {
  6.     printf(/* cmt1 */"%d" /* cmt2 */ "\n", 10);  /*
  7. */
  8.  
  9.     char* str = "foo \
  10.                  jedi" /* cmtX */ " \
  11.                  bar";
  12.  
  13.     printf("what /* about this */ pal?\n");
  14.  
  15.     char arr[3] = {'a' /* cmt3 */, 'b', /* cmt4 */ 'c'}; // remove me
  16.     // Remove ME TOO PLEASE!!!
  17.  
  18.     return 0;
  19. }
  20.  
  21. /* A multi
  22.  * line
  23.  * comment
  24. */
  25.  
  26. // mark ini, mark end positions
  27. // IN  \n OUT
  28.  
  29. /* IN */ //OUT, but not if IN string.

C Program to Remove Single Space or Line

How to write a C Program to Remove Single Space or Line in C Programming Language ?

Solution For C Program :

/*C Program to Remove Single Space or Line.*/

#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<dos.h>
void main()
{
FILE *a,*b;
char fname[20],ch,tch=NULL,tch1=NULL;
int flag1=0,flag=0,count=0,count1=0,count2=0;
clrscr();
printf(“Enter the file name (.C or .TXT)\n”);
gets(fname);
a=fopen(fname,”r”);
if(a==NULL)
{
puts(“Cannot open the source file!!!”);
delay(500);
exit(1);
}
b=fopen(“target.c”,”w”);
if(b==NULL)
{
puts(“Cannot create target file!!!”);
delay(500);
exit(1);
}
while(1)
{
ch=fgetc(a);
if(ch==EOF) break;
else
{
if(ch==’\n’)
{
count1=1;
tch1=ch;
continue;
}
else if(ch==’\n’&& count1==1)
continue;
else if(ch==’/'&&count==0)
{
flag1=1;
tch=ch;
count=1;
continue;
}
else if(ch==’*'&& flag1==1)
{
flag=1;
flag1=0;
tch=NULL;
}
else if(ch==’*'&&flag==1)
{
count2=1;
count=1;
continue;
}
else if(ch==’/'&&count2==1)
{
flag=0;
count=0;
tch=NULL;
continue;
}
else if(count==1&&flag==1)
count=0;
else if(flag1==1)
flag1=0;
}
if(flag!=1)
{
if(tch>0)
fputc(tch,b);
if(tch1>0)
fputc(tch1,b);
tch1=NULL;
tch=NULL;
count=0,count1=0,count2=0;
fputc(ch,b);
flag1=0,flag=0;
}
}
puts(“DONE!! OP FILE IS \”TARGET.C\”\n”);
fcloseall();
getch();
}