Question

In: Computer Science

Complete Question 1a-c 1a) Write a C program that displays all the command line arguments that...

Complete Question 1a-c

1a) 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

1b) Write a C program that reads in a string from the keyboard.
Use scanf with the conversion code %s. Recall that the 2nd
arg in scanf should be the address of the location in
memory into which the inputted string should be stored.
Recall that the name of an array without square brackets is
the address of the first slot of the array.

1c) Write a program that reads in this file (example.txt) and counts
and displays the number of lowercase e's in the file. Use the
fread function to read the entire file with just one call of fread.
Use google to get info on fread.

Solutions

Expert Solution

Part a)

C code:

#include <stdio.h>

//argc stores number of command line arguments

//char** argv is an array of strings that stores arguments

//argv[0] is the name of the program itself

int main(int argc, char** argv) {

    //loop through all arguments and print them to console

    int i;

    for(i = 0; i < argc; ++i) {

        printf("%s\n", argv[i]);

    }  

    return 0;

}

Sample IO:

Part B)

C code:

#include <stdio.h>

int main(int argc, char** argv) {

    char str[100]; // declaring character array (string)

    scanf("%s", str); // take input from console

    

    printf("%s\n", str); //print string to screen (optional)

    return 0;

}

Sample IO:

Part C)

C Code:

#include <stdio.h>

int main() {

    FILE* infile; // file pointer for input file stream

    infile = fopen("examples.txt", "r"); // open examples.txt in read mode

    char buffer[1000]; // buffer will store data read from file

    //using fread() to read one character at a time and maximum 500(or whatever you want)

    // times from infile. fread() returns the total number of successful reads;

    int charCount = fread(buffer, 1, 500, infile);

    int low_e_count = 0; // variable to count lowercase 'e'

    // loop through all characters and count lowercase 'e'

    int i;

    for(i = 0; i < charCount; ++i) {

        if(buffer[i] == 'e') low_e_count++;

    }

    // print answer to console

    printf("Number of lowercase 'e' in examples.txt = %d\n", low_e_count);

}

Sample IO:

examples.txt:

Console:



Related Solutions

program c Write a program called filesearch that accepts two command-line arguments: A string A filename...
program c Write a program called filesearch that accepts two command-line arguments: A string A filename If the user did not supply both arguments, the program should display an error message and exit. The program opens the given filename. Each line that contains the given string is displayed. Use the strstr function to search each line for the string. You may assume no line is longer than 255 characters. The matching lines are displayed to standard output (normally the screen).
Write a program that takes two command line arguments at the time the program is executed....
Write a program that takes two command line arguments at the time the program is executed. You may assume the user enters only decimal numeric characters. The input must be fully qualified, and the user should be notified of any value out of range for a 23-bit unsigned integer. The first argument is to be considered a data field. This data field is to be is operated upon by a mask defined by the second argument. The program should display...
Write a program that prints the sum of its command-line arguments (assuming they are numbers). For...
Write a program that prints the sum of its command-line arguments (assuming they are numbers). For example, java Adder 3 2.5 -4.1 should print The sum is 1.4
Write a Java program which reads a positive integer from the command line, then displays the...
Write a Java program which reads a positive integer from the command line, then displays the sum of all even values up to and including the value provided, followed by the sum of all odd values up to and including the value provided. validate that the command line argument is an integer greater than 0 to validate the type, you can use Integer.parseInt() with a try/catch for NumberFormatException use one or more for loops to perform the even and odd...
write the following C program that ccepts command-line arguments following argv[0] — up to 20 decimal...
write the following C program that ccepts command-line arguments following argv[0] — up to 20 decimal integer literals. Each of these decimal integer literals will be expressed using decimal digits, only, and none of the decimal integer literals will be prefixed by a plus or minus sign. None of the decimal integer literals will be greater than ULONG_MAX. The program prints the integers in order, least-to-greates and also prints the sum of the integers.
Introduction Write in C++ at the Linux command line a program that is the same as...
Introduction Write in C++ at the Linux command line a program that is the same as the previous collection app project but now uses a class to store the items and also can save the items to a file that can be read back into the array by the user when the program is re-started. You can use your project 1 submission as a starting point or you can do something new as long as it meets the listed requirements....
IN C LANGUAGE This program takes two command line arguments: an input filename a threshold Your...
IN C LANGUAGE This program takes two command line arguments: an input filename a threshold Your program will create two files: even.txt - contains all integers from the input file that are even and greater than the threshold odd.txt - contains all integers from the input file that are odd and greater than the threshold The input file will exist and only contain a set of integers. It will always be valid data. Output whitespace will be ignored. Name the...
Write a program Median.java to read each file whose name is specified in the command-line arguments....
Write a program Median.java to read each file whose name is specified in the command-line arguments. That is, for each command-line argument, open it as a file and read it. The file contents are zero or more lines each containing a list of comma-separated integers, such as 1,2,3,4 or 99,120,33. You should parse each of these integers and save them in an ArrayList (if you prefer you may use an array, but an ArrayList is likely to be easier for...
(Intro/Basic) JAVA Write a small program that gets some numbers as command-line arguments and finds the...
(Intro/Basic) JAVA Write a small program that gets some numbers as command-line arguments and finds the maximum of those numbers. You can assume that the user will provide some command-line arguments when running the program (so you don’t have to worry about what to do if the user doesn’t provide any arguments -- that won’t happen). Also, you can assume that all the command line arguments will be floating-point numbers, i.e., numbers with a decimal point, like 2.95.
Write a script named countmatches that expects at least two arguments on the command line. The...
Write a script named countmatches that expects at least two arguments on the command line. The first argument is the pathname of a dna file containing a valid DNA string with no newline characters or white space characters of any kind within it. (It will be terminated with a newline character.) This dna file contains nothing but a sequence of the bases a, c, g, and t in any order. The remaining arguments are strings containing only the bases a,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT