Question

1) Write a C program that displays all the command line arguments that appear on the...

1) Write a C program that displays all the command line arguments that
   appear on the command line when the program is invoked. Use the
   file name cl.c for your c program. Test your program with

   cl hello goodbye
     and
   cl 1 2 3 4 5 6 7 8
     and
   cl

2) Write a C program which displays the sum of the command line
   arguments. Hint: use sscanf to convert the decimal arguments 
   (which are strings) to binary numbers that can be added. Use
   the file name sum.c for your program. Test your program with

   sum 5 -10 3
      and
   sum 8
      and
   sum

3) Write a C program that reads in a file and outputs it in
   reverse order. Use recursion. Test your program with the
   file reverse.txt (on our web site).  Hint: Structure of
   the recursive function reverse that you need:

      if not at end of file
          read line
          save line in a safe area (use strdup to do this)
          call reverse
          display line that was saved

   Use fgets to read line. Recall fgets returns 0 (which represents
   false) on end of file.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

1.

// command line arguments
#include<stdio.h>

int main(int argc,char* argv[])
{
   int i;
   if(argc==1)
       printf("\nNo Extra Command Line Argument");
   if(argc>=2)
   {
       printf("\nNumber Of Arguments Passed: %d",argc); //Including file name
       printf("\nThe Command Line Arguments Passed are ");
       for(i=0;i<argc;i++)
           printf("\n %s",argv[i]);
   }
   printf("\n");
   return 0;
}

Output:

2.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc,char* argv[])
{
   int i,sum=0;
   int arr[argc-1];
   if(argc == 1)
       printf("\nNo numbers are entered");
   if(argc>=2)
   {
       for(i=0;i<argc;i++)
       {
           sscanf(argv[i],"%d",&arr[i-1]);
       }
       for(i=0;i<argc-1;i++)
           sum= sum + arr[i];
       printf("\nSum of the given numbers is %d",sum);
   }
   printf("\n");
}

Output:

3.

#include<stdio.h>
#include <string.h>

char* reverse(FILE *fp,char *str)
{
   char *p,*q,*res;
   if( fgets (str, 60, fp)!=NULL )
   {  
       q=strdup(str);
       reverse(fp,str);
       printf("%s",q);
   }
else
{
return "";
   }
   return str;
}

int main () {
FILE *fp;
char str[60];
char *res;

/* opening file for reading */
fp = fopen("file.txt" , "r");
res=reverse(fp,str);
fclose(fp);

return(0);
}

Output:

If you have any queries, please comment below.

Please upvote , if you like this answer.

Add a comment
Know the answer?
Add Answer to:
1) Write a C program that displays all the command line arguments that appear on the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • After reading pages 330 - 336, write a program that takes two command line arguments which...

    After reading pages 330 - 336, write a program that takes two command line arguments which are file names. The program should read the first file line by line and write each line, in reverse order, into the second file. The program should include a "usage" method that displays the correct command line syntax if two file names are not provided. Example: if my input file says Hello, World! then my output file will contain !dlroW ,olleH Hints: Use CaesarCipher...

  • C programming Question1 (a) Write a C program that will print out all command line arguments,...

    C programming Question1 (a) Write a C program that will print out all command line arguments, in reverse order, one per line. Prefix each line with its index. 6 marks] (b) Consider this C code snippet int a- 100 int b- 42; inte p- &a; int q-b; p qi printf ("%d %d\n" ,a,*p); When this code is executed, what numbers will it print? [2 marks] (c) Consider this C program int main(int argc,char argv) char* target- "Apple" char vord[100] printf...

  • Write a full program that will accept any number of command line arguments and print them...

    Write a full program that will accept any number of command line arguments and print them to the screen. Suppose your program is called Print.java. When the command java Print hello goodbye you is run, the output will be hello goodbye you Write a full program that will accept exactly  three command line arguments that can be interpreted as integers, and will add them up. If there aren't exactly 3 command line arguments, print an error message and terminate the program....

  • Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text...

    Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text file by removing all blank lines (including lines that only contain white spaces), all spaces/tabs before the beginning of the line, and all spaces/tabs at the end of the line. The file must be saved under a different name with all the lines numbered and a single blank line added at the end of the file. For example, if the input file is given...

  • Problem: Write a program that behaves as described below.If the first command-line argument after the program...

    Problem: Write a program that behaves as described below.If the first command-line argument after the program name (argv[1]) is “--help”, print the usage information for the program. If that argument is not “--help”, you are to expectargv[1]and subsequent arguments to be real numbers(C, integer, float, or double types)in formats acceptable to the sscanf()function of the C library or strings of ASCII chars that are not readable as real numbers. You are to read the numbers, count them and calculate the...

  • Using network sockets, write a C program called client that receives three command-line arguments in the...

    Using network sockets, write a C program called client that receives three command-line arguments in the form: client host port file and sends a request to a web server. The command-line arguments are hostRepresents the web server to connect to port Represents the port number where a request is sent. Normally an HTTP request is sent over port 80, but this format allows for custom ports file Represents the file requested from the web server Your program should create a...

  • Write a C program that reads in the file and outputs it to another file with...

    Write a C program that reads in the file and outputs it to another file with its lines in reverse order. Use the following array to store the addresses of the lines as you read them in: char *lines[200]; Save each line so that it occupies only enough memory required by that line. Use recursion. Hint: On each call of the recursive function, read ONE line into a LOCAL buffer.

  • Your task is to write a C++ program that consumes integer values as command line arguments...

    Your task is to write a C++ program that consumes integer values as command line arguments and returns the arithmetic mean of these values. To increase the flexibility of the program, there should be no set number of arguments. To overcome this, we will require the argument argv[1] to be the number of integers the user enters. For example, if the user wants to calculate the arithmetic mean of 4 numbers, they would pass in 4 as the first argument...

  • Programs/scripts 13. Write a bash shell script that displays the date by using the shell command...

    Programs/scripts 13. Write a bash shell script that displays the date by using the shell command date. 14. Write a command line program that takes a number and a command as arguments. Call that command that number of times. For example, if your program name is "test" test 3 dir <shows directory> dir <shows directory> dir <shows directory>

  • Command Line Arguments: Write a program in C Compiler that will accept two integers on command...

    Command Line Arguments: Write a program in C Compiler that will accept two integers on command line, subtract the second from the first (1st - 2nd) and display the answer. It exactly two numbers are not provided on command line, there should be a simple error message printed and the program ends with no further input, output, or calculations

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT