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
1
Expert's answer
2016-04-15T08:18:04-0400
#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 the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS