Question

In: Computer Science

16.15 Lab 5: filter Name this program filter.c. The program takes two command line arguments: the...

16.15 Lab 5: filter

Name this program filter.c.

  • The program takes two command line arguments: the name of an input file and the name of an output file. The program should confirm the input and output files can be opened. If a file cannot be opened, print the error message Cannot open file '<filename>' where <filename> is the name of the file and return.
    • fopen() will return 0 if it fails to open a file.
  • Then, the program will read the file string by string (up to 50 characters in length). If the string consists of only uppercase and lowercase characters, print it.
    • You can use isalpha(char) declared in ctype.h to check whether a character is a-z or A-Z.
    • continue and break might be useful.
  • Don't forget to close your files!

Examples

./a.out example.txt output.txt
example.txt output.txt
The Quick Brown Fox Jumps over the Lazy Old Dog. ALABAMA? Roll Tide!!! P2P 1831 (UA) The Quick Brown Fox Jumps over the Lazy Old Roll
./a.out does_not_exist.txt output.txt
Cannot open file 'does_not_exist.txt'

Solutions

Expert Solution

#include <stdio.h> 
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <stdbool.h>
int main(int argc, char** argv) 
{ 
        FILE *fptr1, *fptr2; 
    char c; 
    char str[400];
    char newString[20][20]; 
    char b[20];


        // Open one file for reading 
        fptr1 = fopen(argv[1], "r"); 
        if (fptr1 == NULL || fptr1 == 0) 
        { 
                printf("Cannot open file %s \n", argv[1]); 
        } 


        // Open another file for writing 
        fptr2 = fopen(argv[2], "w"); 
        if (fptr2 == NULL || fptr2 == 0) 
        { 
                printf("Cannot open file %s \n", argv[2]); 
        } 

        // Read contents from file 
        c = fgetc(fptr1);   
  int x=0;
   while (c != EOF) 
        { str[x++]=c;
      c = fgetc(fptr1); 
        } 

  
    int i,j,ctr;
    j=0; ctr=0;
    for(i=0;i<=(strlen(str));i++)
    {
        // if space or NULL found, assign NULL into newString[ctr]
        if(str[i]==' '||str[i]=='\0')
        {
            newString[ctr][j]='\0';
            ctr++;  //for next word
            j=0;    //for next word, init index to 0
        }
        else
        {
            newString[ctr][j]=str[i];
            j++;
        }
    }
  for(j=0;j<=ctr;j++)
  {
     for(i=0;i<=(strlen(newString[j])-1);i++)
    {
       if( isalpha(newString[j][i])==0)
       {
           b[j]='*';
           break;
       }
         
  }
  }

char space[1]=" ";

 for(j=0;j<ctr;j++)
  {
    if(b[j]!='*'){
        
     for(i=0;i<=(strlen(newString[j])-1);i++)
    {
        fputc(newString[j][i], fptr2); 
    }
         fputc(space[0], fptr2); 

  }
  }

        fclose(fptr1); 
        fclose(fptr2);
                return 0; 
}

Related Solutions

IN C LANGUAGE 16.15 Lab 5: filter Name this program filter.c. The program takes two command...
IN C LANGUAGE 16.15 Lab 5: filter Name this program filter.c. The program takes two command line arguments: the name of an input file and the name of an output file. The program should confirm the input and output files can be opened. If a file cannot be opened, print the error message Cannot open file '<filename>' where <filename> is the name of the file and return. fopen() will return 0 if it fails to open a file. Then, the...
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...
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 ordered.java Order check Write a program Ordered.java that takes four int command-line arguments...
write a program ordered.java Order check Write a program Ordered.java that takes four int command-line arguments w, x, y, and z. Define a boolean variable whose value is true if the four values are either in strictly ascending order (w < x < y < z) or strictly descending order (w > x > y > z), and false otherwise. Then, display the boolean variable value. NOTE 1: Do not use if statements on this program. NOTE 2: Assume that...
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...
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 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 C++ program that prints out all of the command line arguments passed to the...
Write a C++ program that prints out all of the command line arguments passed to the program. Each command line argument should be separated from the others with a comma and a space. If a command line argument ends in a comma, then another comma should NOT be added
Write a program that takes an integer N from the command line and uses StdRandom.uniform() to...
Write a program that takes an integer N from the command line and uses StdRandom.uniform() to generate a random sequence of integers be- tween 0 and N – 1. Run experiments to validate the hypothesis that the number of integers generated before the first repeated value is found is ~√?N/2.
Using the main function’s arguments, create a program that takes in a person’s name, their home...
Using the main function’s arguments, create a program that takes in a person’s name, their home town/location, and cell number then prints out the following message: Sample run 1: Java lab01_task03 Sililo Uis 0819876543 Output:   Contact successfully saved !! Contact Name : Sililo @ Uis Home number: 0819876543
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT