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

Hello, I need help with this problem in C. I am trying to get the code to do wha

ID: 3708602 • Letter: H

Question

Hello, I need help with this problem in C. I am trying to get the code to do what the instructions say but also with it using STDIN and STDOUT and it has to be that the user can type in ./a.out <input.txt and it will display the results on the screen. It has to be paired with a .txt file. Please provide a sample output. It has to take in a .txt file containing the information.

MY CODE SO FAR(NEED TO KEEP WHAT I HAVE FOR STDIN AND STDOUT AS I AM TRYING TO USE THIS METHOD)

#include<stdio.h>

#include<stdlib.h>

#define MAXLEN 100

int main(int argc, char **argv)

{

//freopen(argv[1], "r" , stdin);

int bufferlen=MAXLEN;

char *buffer=malloc(MAXLEN);

  

if(buffer==NULL)//To check if NULL

{

printf("ERROR: Corrupt malloc. Terminating. ");//Prints error message

return 1;//Terminates program with 1 value

}

while(fgets(buffer,bufferlen,stdin)!=NULL)//Will take in first line and put it into buffer

{

printf("%s",buffer);//Print the strings

scanf("%s",buffer);

if (ferror(stdin))//In case of error

{

printf("Error on stdin. ");

break;

}

}

}

Show transcribed image text

Mind Your P's and Q's Filename: PANDQ The Problem: Given a list of strings, count the number of occurrences of the letters P and Q in each The Input: On each line will be one string, starting in column one. The strings are guaranteed to be between one and seventy characters in length, inclusive. The only characters that will appear in the strings are capi- tal letters The Output: For each input string, print the number of P's and Q's, in the following format: where n is the number of P's, and m is the number of Q's. Sample Input: GJKRYQRGHBHUBHKOPPUBHIGUGYUGYTHIHK THISHASNONEOFEITHER Sample Output: 2 PS, 1 Q'S 12 P'S, 15 Q's 0 P'S, 0 Q'S

Explanation / Answer

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#define MAXLEN 100

void count_p_and_q(char *buffer)

{

                /* Used to traverse string */

                char *str = NULL;

                /* Used to count number of P's and Q's in a string */

                int no_of_p = 0;

                int no_of_q = 0;

                str = buffer;

                no_of_p = 0;

                no_of_q = 0;

                while (*str != '') {

                                if (*str == 'P')

                                                no_of_p++;

                                else if (*str == 'Q')

                                                no_of_q++;

                                str++;

                }

                printf("%d P'S %d Q'S ", no_of_p, no_of_q);

}

int main(int argc, char **argv)

{

                FILE *fp = NULL;

                /* To store string */

                char *buffer = NULL;

                if (argc != 2) {

                                printf("Usage1: ./a.out input.txt (or) ");

                                printf("Usage2: "./a.out stdin", Enter "EXIT" after finish ");

                                return 1;

                }

                buffer = calloc(1, MAXLEN);

                if (!buffer) {

                                perror("calloc");

                                return 1;

                }

                /*

                * If user file input is stdin read the strings from user input

                * else open a file and read from it.

                */

                if (strcmp(argv[1], "stdin") == 0) {

                                while (fscanf(stdin, "%s", buffer) != EOF) {

                                                /*

                                                * If the user entered string is EXIT

                                                * then quit the program

                                                */

                                                if (strcmp(buffer, "EXIT") == 0)

                                                                break;

                                                count_p_and_q(buffer);

                                                memset(buffer, 0, MAXLEN);

                                }

                } else {

                                /* Open a file in reading mode */

                                fp = fopen(argv[1], "r");

                                if (!fp) {

                                                perror("fp");

                                                return 1;

                                }

                                /* Read a string from the file */

                                while (fscanf(fp, "%s", buffer) != EOF)

                                                count_p_and_q(buffer);

                }

                free(buffer);

                return 0;

}

Sample Output:

My_PC$ cat input.txt

GJKRYQRGHBHJBHKOPPIJBHJGUGYUGYIHIHK

PPPPPPPPPQQQQQQQQQQQPPPQQQQ

THISHASNONEOFEITHER

My_PC$ ./a.out input.txt

2 P'S 1 Q'S

12 P'S 15 Q'S

0 P'S 0 Q'S

My_PC$ ./a.out stdin

PKDLSKSJAQPEPQPELSKQP

5 P'S 3 Q'S

LSKDPQLSKDIALWKEQ

1 P'S 2 Q'S

NOMATCH

0 P'S 0 Q'S

EXIT

My_PC$

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote