Hello, I need help with this problem in C. I am trying to get the code to do wha
ID: 3708541 • 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;
}
}
}
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'SExplanation / Answer
//You can code it the following way:-
//Command to run this would be
// ./a.out < input.txt
#include<stdio.h>
#include<stdlib.h>
#define MAXLEN 100
int main(int argc, char **argv)
{
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
}
FILE *fp = freopen(argv[1], "r" , stdin);
if (fp == NULL)
{
printf("Could not open file %s",argv[1]);
return 1;
}
while (fgets(buffer, MAXLEN, fp) != NULL)//Will take input line by line and put it into buffer
printf("%s", buffer);//Print the buffer
fclose(fp);
}
/*
Sample input:
PPPFEMSOCQNIUWPFEND
PFEOJNQQQVE
GMRIUEQQINFEKN
Sample output:
PPPFEMSOCQNIUWPFEND
PFEOJNQQQVE
GMRIUEQQINFEKN
*/
//================================================
// This is complete solution for the problem:-
#include<stdio.h>
#include<stdlib.h>
#define MAXLEN 100
int main(int argc, char **argv)
{
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
}
FILE *fp = freopen(argv[1], "r" , stdin);
if (fp == NULL)
{
printf("Could not open file %s",argv[1]);
return 1;
}
while (fgets(buffer, MAXLEN, fp) != NULL)//Will take input line by line and put it into buffer
{
int i=0,PCount=0,QCount=0;
while(buffer[i]!='')//Iterates till the end of the line
{
if(buffer[i]=='P')//increment the count of 'P'
PCount++;
if(buffer[i]=='Q')//increment the count of 'Q'
QCount++;
i++;
}
printf("%d P's, %d Q's ",PCount,QCount);
}
fclose(fp);
}
/*
Sample input:
PPPFEMSOCQNIUWPFEND
PFEOJNQQQVE
GMRIUEQQINFEKN
Sample output:
4 P's, 1 Q's
1 P's, 3 Q's
0 P's, 2 Q's
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.