Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

mycat must receive at least one parameter, file1, whose content will be printed

ID: 3595116 • Letter: M

Question

mycat must receive at least one parameter, file1, whose content will be printed on the screen. If more than one file name is specified, the contents of all of the files will be printed.

mycat must accept any combination of the following switches:

-e   Display a dollar sign ('$') at the end of each line.

-n   Number the output lines, starting at 1.

-s   Squeeze (remove) all empty lines, causing the output to be single spaced. An empty line can contain one or more whitespace characters (e.g. space, tab, newline, carriage return).

Explanation / Answer

#include<stdio.h>
#include<string.h>
void help_msg()
{
printf("Invalid usage: mycat [-e][-n][-s] filename [,filename ... ] ");
printf(" ");
printf("-e   Display a dollar sign ('$') at the end of each line. ");
printf("-n   Number the output lines, starting at 1. ");
printf("-s   Squeeze (remove) all empty lines, causing the output to be single spaced. ");
}

int main(int argc, char *argv[])
{
   int no_of_choice;
   FILE *fp;
   int i,valid=1;
   char line[1024],line1[1024];
   char ch1,ch2,ch3,ch;

   if ( argc < 2 )
    {
      help_msg();
      return -1;
    }
   else
    {
       if (argv[1][0]=='-')
        {
           if(argc<3)
            {
              help_msg();
              return -2;
            }
           else
            {
                int choices=strlen(argv[1]);
                int i;
                for(i=1;i<choices;i++)
                 {
                     switch(argv[1][i])
                      {
                        case 'e': ch1='e'; break;
                        case 'n': ch2='n'; break;
                        case 's': ch3='s'; break;
                         default:
       help_msg();
                            valid=0;
                return -3;
                       } // End of switch case
                 } // End of for loop
             } // End of if (argc<3)
          }// End of if (argv[1][0]=='-'
      } // End of argc < 2


    no_of_choice=ch1=='e'||ch2=='n'||ch3=='s'?1:0;

if(no_of_choice==0)
   {
      for(i=1;i<argc;i++)
       {
          fp=fopen(argv[i],"r");
          if(fp==NULL)
           {
             printf("Unable to open a file - %s ", argv[i]);
           }
          else
           {
             printf("%s - file contents are .... ",argv[i]);
             while(1)
              {
                 ch=fgetc(fp);
                 if(ch==EOF) break;
                 printf("%c",ch);
              }
              fclose(fp);
           }
       }
     }
else
   {
      if(valid)
       { int l;
          for(i=2;i<argc;i++)
           {
              fp=fopen(argv[i],"r");
              l=0;
              if(fp==NULL)
               {
                   printf("Unable to filename - %s ", argv[i]);
               }
              else
               {
                  int j=0;
                  printf("%s - file contents are .... ",argv[i]);
                   while(1)
                    {
                        ch=fgetc(fp);
                       if(ch==EOF) break;
                       if(ch!=' ')
                         line[j++]=ch;
                       else
                         {
                            if(ch3=='s')
                              if((ch==' '||ch==' ')&&j==0)
                                  continue;

                            line[j]='';
                            if (ch2=='n') {
                              sprintf(line1,"%03d    %s",++l,line);
                              strcpy(line, line1);
                             }
                            if(ch1=='e')
                               sprintf(line,"%s$",line);
                             j=0;
                            printf("%s ",line);
                         }
                    }
                    fclose(fp);
               }
           }
       }
   }
   return 0;
}

/*
lenovo@lenovo-Vbox:~/chegg$ cc mycat.c -o mycat
lenovo@lenovo-Vbox:~/chegg$ cat info.txt
0 132.5
0.1 147.2
0.2 148.3
0.3 157.3
0.4 163.2
0.5 158.2
0.6 169.3
0.7 148.2
0.8 137.6
0.9 135.9
lenovo@lenovo-Vbox:~/chegg$ cat keyboard.txt
a-z keys

small case

upper case

numeric key pad

function keys

toggle keys

dead keys
lenovo@lenovo-Vbox:~/chegg$
lenovo@lenovo-Vbox:~/chegg$ ./mycat
Invalid usage:
  mycat [-e][-n][-s] filename [,filename ... ]


-e   Display a dollar sign ('$') at the end of each line.
-n   Number the output lines, starting at 1.
-s   Squeeze (remove) all empty lines, causing the output to be single spaced.
lenovo@lenovo-Vbox:~/chegg$ ./mycat -efs
Invalid usage:
  mycat [-e][-n][-s] filename [,filename ... ]


-e   Display a dollar sign ('$') at the end of each line.
-n   Number the output lines, starting at 1.
-s   Squeeze (remove) all empty lines, causing the output to be single spaced.
lenovo@lenovo-Vbox:~/chegg$ ./mycat -efs hello
Invalid usage:
  mycat [-e][-n][-s] filename [,filename ... ]


-e   Display a dollar sign ('$') at the end of each line.
-n   Number the output lines, starting at 1.
-s   Squeeze (remove) all empty lines, causing the output to be single spaced.
lenovo@lenovo-Vbox:~/chegg$ ./mycat -ens hello
Unable to filename - hello
lenovo@lenovo-Vbox:~/chegg$
lenovo@lenovo-Vbox:~/chegg$ ./mycat info.txt keyboard.txt
info.txt - file contents are ....
0 132.5
0.1 147.2
0.2 148.3
0.3 157.3
0.4 163.2
0.5 158.2
0.6 169.3
0.7 148.2
0.8 137.6
0.9 135.9
keyboard.txt - file contents are ....
a-z keys

small case

upper case

numeric key pad

function keys

toggle keys

dead keys
lenovo@lenovo-Vbox:~/chegg$

lenovo@lenovo-Vbox:~/chegg$ ./mycat -e keyboard.txt
keyboard.txt - file contents are ....
a-z keys$
$
small case$
$
upper case$
$
numeric key pad$
$
function keys$
$
toggle keys$
$
dead keys$
lenovo@lenovo-Vbox:~/chegg$ ./mycat -e info.txt
info.txt - file contents are ....
0 132.5$
0.1 147.2$
0.2 148.3$
0.3 157.3$
0.4 163.2$
0.5 158.2$
0.6 169.3$
0.7 148.2$
0.8 137.6$
0.9 135.9$
lenovo@lenovo-Vbox:~/chegg$

lenovo@lenovo-Vbox:~/chegg$ ./mycat -n info.txt
info.txt - file contents are ....
001    0 132.5
002    0.1 147.2
003    0.2 148.3
004    0.3 157.3
005    0.4 163.2
006    0.5 158.2
007    0.6 169.3
008    0.7 148.2
009    0.8 137.6
010    0.9 135.9
lenovo@lenovo-Vbox:~/chegg$ ./mycat -n keyboard.txt
keyboard.txt - file contents are ....
001    a-z keys
002   
003    small case
004   
005    upper case
006   
007    numeric key pad
008   
009    function keys
010   
011    toggle keys
012   
013    dead keys
lenovo@lenovo-Vbox:~/chegg$
lenovo@lenovo-Vbox:~/chegg$ ./mycat -n keyboard.txt info.txt
keyboard.txt - file contents are ....
001    a-z keys
002   
003    small case
004   
005    upper case
006   
007    numeric key pad
008   
009    function keys
010   
011    toggle keys
012   
013    dead keys
info.txt - file contents are ....
001    0 132.5
002    0.1 147.2
003    0.2 148.3
004    0.3 157.3
005    0.4 163.2
006    0.5 158.2
007    0.6 169.3
008    0.7 148.2
009    0.8 137.6
010    0.9 135.9
lenovo@lenovo-Vbox:~/chegg$
lenovo@lenovo-Vbox:~/chegg$ ./mycat -ns keyboard.txt info.txt
keyboard.txt - file contents are ....
001    a-z keys
002    small case
003    upper case
004    numeric key pad
005    function keys
006    toggle keys
007    dead keys
info.txt - file contents are ....
001    0 132.5
002    0.1 147.2
003    0.2 148.3
004    0.3 157.3
005    0.4 163.2
006    0.5 158.2
007    0.6 169.3
008    0.7 148.2
009    0.8 137.6
010    0.9 135.9
lenovo@lenovo-Vbox:~/chegg$
lenovo@lenovo-Vbox:~/chegg$ ./mycat -nse keyboard.txt
keyboard.txt - file contents are ....
001    a-z keys$
002    small case$
003    upper case$
004    numeric key pad$
005    function keys$
006    toggle keys$
007    dead keys$
lenovo@lenovo-Vbox:~/chegg$ ./mycat -nse info.txt
info.txt - file contents are ....
001    0 132.5$
002    0.1 147.2$
003    0.2 148.3$
004    0.3 157.3$
005    0.4 163.2$
006    0.5 158.2$
007    0.6 169.3$
008    0.7 148.2$
009    0.8 137.6$
010    0.9 135.9$
lenovo@lenovo-Vbox:~/chegg$ ./mycat -nse info.txt keyboard.txt
info.txt - file contents are ....
001    0 132.5$
002    0.1 147.2$
003    0.2 148.3$
004    0.3 157.3$
005    0.4 163.2$
006    0.5 158.2$
007    0.6 169.3$
008    0.7 148.2$
009    0.8 137.6$
010    0.9 135.9$
keyboard.txt - file contents are ....
001    a-z keys$
002    small case$
003    upper case$
004    numeric key pad$
005    function keys$
006    toggle keys$
007    dead keys$
lenovo@lenovo-Vbox:~/chegg$



*/