Practice

Just Inline Comments



Design and code a function named justInlineComments() with the header

 int justInlineComments(const char *filename)

Your function displays all of the C-style comments in a text file.  filename points to a null-terminated string that contains the name of the file.  Your function displays multi-line comments as they appear in the file.  Your function returns 0 if successful, 1 if the file is not found. 

You may assume that each comment appears on a separate line and starts in the first column of that line. You may also assume that there are no input errors while reading the file. 

Consider the following file

 // Sample File
 // source.c
 //
 int main(void)
 {
         int i;
         char s[100];

         // prompt for input
         printf("Enter a string : ");
         // accept a string
         scanf("%s", s);
         // display the string entered 
         printf("%s\n", s);

         return 0;
 }

The statement

 justInlineComments("source.c");

displays

 // Sample File
 // source.c
 //
 // prompt for input
 // accept a string
 // display the string entered

As an additional challenge, upgrade your function so that each comment displays in the same position as the comment appears in the source file.  That is, if a comment starts in column 8, your upgraded function displays the comment starting in column 8.  You may assume that each tab character in the source file expands to 8 spaces on display. 

For example,

 // Sample File
 // source.c
 //
         // prompt for input
         // accept a string
         // display the string entered



  Top  Go Back to the Top of this Page



  Designed by Chris Szalwinski   Copying From This Site