Question

In: Computer Science

Write a C program that can search for a string within a text file, replace it...

Write a C program that can search for a string within a text file, replace it with another string and put results in a new file.

Solutions

Expert Solution

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void main()
{
//variable declaration
FILE *fptr, *outputfp;
char ch;
int wrd=1,charctr=1;
char fname[20];

//open file
fptr=fopen("inputFile.txt","r");
outputfp = fopen("outputFile.txt", "w");
char str[100];
char newStr[100];
char searchStr[] = "how";
char replaceStr[] = "done";
  
if(fptr==NULL || outputfp==NULL)
{
printf(" File does not exist or can not be opened.");
}
else
{
ch=fgetc(fptr);
int i = 0;
  
char c;
while(ch!=EOF)
{
str[i++] = ch;
ch=fgetc(fptr);
}
str[i++] = '\0';
}
  
int i = 0, j = 0;
int flag = 0, start = 0;
while (str[i] != '\0')
{
if (str[i] == searchStr[j])
{
if (!flag)
start = i;
j++;
if (searchStr[j] == '\0')
break;
flag = 1;
}
else
flag = start = j = 0;
i++;
}
  
  
if (searchStr[j] == '\0' && flag)
{
for (i = 0; i < start; i++)
putc(str[i], outputfp);

for (j = 0; j < strlen(replaceStr); j++)
{
putc(replaceStr[j], outputfp);
i++;
}

for (j = start + strlen(searchStr); j < strlen(str); j++)
{
putc(str[j], outputfp);
i++;
}

newStr[i] = '\0';
}
  
//close file
fclose(fptr);
fclose(outputfp);
}

INPUT:

inputFile.txt

Search for a string = "how"

Replace with a string = "done"

outputFile.txt


Related Solutions

Write a C++ program to create a text file. Your file should contain the following text:...
Write a C++ program to create a text file. Your file should contain the following text: Batch files are text files created by programmer. The file is written in notepad. Creating a text file and writing to it by using fstream: to write to a file, you need to open thew file as write mode. To do so, include a header filr to your program. Create an object of type fsrteam. Open the file as write mode. Reading from a...
Write a C program that Reads a text file(any file)  and writes it to a binary file....
Write a C program that Reads a text file(any file)  and writes it to a binary file. Reads the binary file and converts it to a text file.
Write a program to remove extra blanks from text files. Your program should replace any string...
Write a program to remove extra blanks from text files. Your program should replace any string of two or more blanks with one single blank. It should work as follows: * Create a temporary file. * Copy from the input file to the temporary file, but do not copy extra blanks. * Copy the contents of the temporary file back into the original file. * Remove the temporary file. Your temporary file must have a different name than any existing...
Create C# code that can search a text file and output the data at the line...
Create C# code that can search a text file and output the data at the line number inputted and amount of entries needed. Example of call in command window: Search16s filename.txt 273   10 Where 273 is the line number to start the output from, and 10 is the number of sequences that the program should output. The number of sequences entered on call should always be a odd number or give an error in console. The output should also display...
Using C++, write a code that this program always stores text file output into a text...
Using C++, write a code that this program always stores text file output into a text file named "clean.txt". -The program should read one character at a time from "someNumbers.txt", and do the following. -If it is a letter, print that letter to the screen, AND also store it in the text file. All letters should be converted to lowercase beforehand. -If it is a number, print that number to screen, but do NOT store it in the text file....
Write a C program to find out the number of words in an input text file...
Write a C program to find out the number of words in an input text file (in.txt). Also, make a copy of the input file. Solve in C programming.
Could you write a c- program that reads a text file into a linked list of...
Could you write a c- program that reads a text file into a linked list of characters and then manipulate the linked list by making the following replacements 1. In paragraph 1 Replace all “c” with “s” if followed by the characters “e”, “i” or “y”; otherwise 2. In pragraph 2 Replace "We" with v"i" This is the text to be manipulated: Paragraph1 She told us to take the trash out. Why did she do that? I wish she would...
In C Programming Language Write a program to output to a text log file a new...
In C Programming Language Write a program to output to a text log file a new line starting with day time date followed by the message "SUCCESSFUL". Please screenshot the results.
WRITE A C++ PROGRAM TO IMPLEMENT THE CONCEPT OF INDEX (Create index in text file)
WRITE A C++ PROGRAM TO IMPLEMENT THE CONCEPT OF INDEX (Create index in text file)
Could you write a c- program that reads a text file into a linked list of...
Could you write a c- program that reads a text file into a linked list of characters and then manipulate the linked list by making the following replacements 1. Replace all “c” with “s” if followed by the characters “e”, “i” or “y”; otherwise 2. Replace "sh" with ph This is the text to be manipulated: Paragraph1 She told us to take the trash out. Why did she do that? I wish she would not do that Paragraph 2 We...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT