60 468
Assignments Done
98,2%
Successfully Done
In April 2018

Answer to Question #59199 in C for Oz

Question #59199
Write a C program that asks the user for a filename ending in “.txt”. The program should read this file
and then ask the user to input a
search string. The program should tell the user whether the search
string was found or not.
Implement this so that only exact matches are counted. That is, each character in the search string must
be exactly matched in the file
for the program to return success
Expert's answer
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main()
{
char name[50];
char buf[100];
char text[10000];
int fl = 1;


FILE *inf_file;

while(fl)
{
printf("Input name of file: ");
scanf("%s", name);
inf_file = fopen(name, "r");
if(inf_file == NULL)
printf("File not found!\n");
else fl = 0;
}

while(fgets(buf, sizeof(buf), inf_file))
strcat(text, buf);

printf("Input a search string: ");
scanf("%s", buf);

if(strstr(text, buf) == NULL)
printf("search failed!\n");
else printf("String was found! Success!\n");

return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions