129k views
3 votes
Write a program that reads from the external file input.txt, capitalizes all words that begin with the letter "a," and then writes them to an external file output.txt (Note: Do not forget to copy the blanks. You may wish to use infile.get and outfile.put in your program.)

User Tadman
by
5.2k points

2 Answers

2 votes

Answer:

void capitalize(){

int size_buffer = 100;

int i;

FILE * input;

FILE *output;

input = fopen("input.txt", "r");

output = fopen("output.txt", "w");

char * buffer;

buffer = (char*)calloc(size_buffer, sizeof(char))

while(fgets(buffer, size_buffer*sizeof(char), input) != NULL){

if(buffer[0] == 'a'){

for(i = 0; i < strlen(buffer); i++){

buffer[i] -= 32;

}

fprintf(output, "%s\\", buffer)

}

}

fclose(input);

fclose(output);

}

Step-by-step explanation:

I am going to write a c code.

You can use the fgets command to read a line from the txt. I am supposing each line only has a word.

To capitalize a letter, we always subtract the char code by 32. For example 'a' is 97, while 'A' is 65, 'b' is 98 while 'B' is 66... I got these informations from the ascii table.

So

void capitalize(){

int size_buffer = 100;

int i;

FILE * input;

FILE *output;

input = fopen("input.txt", "r");

output = fopen("output.txt", "w");

char * buffer;

buffer = (char*)calloc(size_buffer, sizeof(char))

while(fgets(buffer, size_buffer*sizeof(char), input) != NULL){

if(buffer[0] == 'a'){

for(i = 0; i < strlen(buffer); i++){

buffer[i] -= 32;

}

fprintf(output, "%s\\", buffer)

}

}

fclose(input);

fclose(output);

}

User Temoto
by
4.6k points
4 votes

Answer:

The code is implemented using C++

Step-by-step explanation:

#include <cctype>

#include <fstream>

#include <iostream>

#include <string>

using namespace std;

void OpenFile(string fileName, ifstream& inFile);

void ReadFile(ifstream& inFile, string& fileData);

void ParseFile(string& fileData);

void OutputFile(string fileName, ofstream& outFile, string& fileData);

void main()

{

ifstream inFile;

ofstream outFile;

string fileData;

OpenFile("input.txt", inFile);

ReadFile(inFile, fileData);

ParseFile(fileData);

OutputFile("output.txt", outFile, fileData);

}

void OpenFile(string fileName, ifstream& inFile)

{

inFile.open(fileName);

if (inFile.fail())

{

cerr << "\\Unable to open file \"" << fileName << "\" for reading.\\";

exit(1);

}

}

void ReadFile(ifstream& inFile, string& fileData)

{

while (inFile.good())

{

fileData += inFile.get();

}

inFile.close();

//cout << endl << '[' << fileData[fileData.length()-1] << ']' << endl;

// Remove end of file character

fileData = fileData.substr( 0, fileData.length()-1 );

//cout << endl << '[' << fileData[fileData.length()-1] << ']' << endl;

//cout << endl << fileData << endl;

}

void ParseFile(string& fileData)

{

for(int i = 0; i < fileData.length(); i++)

{

if( fileData[i] == 'a' && isspace((int) fileData[i-1]) &&

( isspace((int) fileData[i+1]) || isalpha((int) fileData[i+1]) ) )

{

fileData[i] = 'A';

}

}

//cout << endl << fileData << endl;

}

void OutputFile(string fileName, ofstream& outFile, string& fileData)

{

outFile.open(fileName);

outFile << fileData;

outFile.close();

}

User Jonasfj
by
5.5k points