Question

In: Computer Science

1. create a .txt file and call it fruits.txt, add the following items to the file...

1. create a .txt file and call it fruits.txt, add the following items to the file

Apple

Banana

Peach

Strawberry

Watermelon

Kiwi

Grape

_______

2. Write a program that does the following:

- Reads the fruit.txt

- Converts the contents into all uppercase

- Writes the uppercased values into a text file called "UpperFruit.txt"

OUTPUT SHOULD BE:

APPLE

BANANA

PEACH

STRAWBERRY

WATERMELON

KIWI

GRAPE

Solutions

Expert Solution

Hi,

Hope you are doing fine. I have coded the above question in C++ keeping all the conditions in mind. The explanation of the code has been clearly provided using comments that have been highlighted in bold. Note that I have also printed the output in console. You may avoid it as per your requirement.

Program:

#include <iostream>
#include <fstream>
#include <algorithm>

using namespace std;

int main()
{
   //fruit is a string that stores each line of the fruits.txt file
   string fruit;
   //upper is an array of string that stores the entire contents of file.
   string upper[100];
   //count is used to count the number of lines in fruits.txt
   int count=0;
   //opening fruits.txt in read mode
   ifstream myfile("fruits.txt");
   //reading each line of file
   while (getline (myfile, fruit)) {
   //storing each line in each index of upper
   upper[count]=fruit;
   //increasing count
   count++;
   }
   //closing file
   myfile.close();
   //converting words in upper to upercase
   for(int i=0;i<count;i++)
   {
       //transform is used along with toupper to convert a string to uppercase
       transform(upper[i].begin(), upper[i].end(), upper[i].begin(), ::toupper);
   }
   //opening file UpperFruit.txt in write mode
   ofstream myfile2("UpperFruit.txt");
   //reading contents of upper
   for(int i=0;i<count;i++)
   {
   //printing output to console
   cout <<upper[i]<<"\n";
   //writing contents to UpperFruit.txt
   myfile2 <<upper[i]<<"\n";
   }
   //closing file
   myfile2.close();
}

fruits.txt

Executable code snippet:

Console output:

Output in file UpperFruit.txt:


Related Solutions

Create a cronjob which does the following tasks 1.) create a file named <yourname>.txt 2.) in...
Create a cronjob which does the following tasks 1.) create a file named <yourname>.txt 2.) in the 35th minute of the hour change the permission to 755 3.) Create a shell script named first.sh in which you print your name and redirect the output by 45th minute of the hour to the text file
IN PYTHON File Data --- In file1.txt add the following numbers, each on its own line...
IN PYTHON File Data --- In file1.txt add the following numbers, each on its own line (20, 30, 40, 50, 60). Do not add data to file2.txt. Write a program. Create a new .py file that reads in the data from file1 and adds all together. Then output the sum to file2.txt. Add your name to the first line in file2.txt (see sample output) Sample Output Your Name 200 use a main function.
Create a c++ program with this requirements: Create an input file using notepad ( .txt )...
Create a c++ program with this requirements: Create an input file using notepad ( .txt ) . When testing your program using different input files, you must change the filename inside your program otherwise there will be syntax errors. There are a finite number of lines to be read from the data file. But we can’t assume to know how many before the program executes; so, the standard tactic is to keep reading until you find the “End of File”...
how to create a script file on puTTy script pp1.txt
how to create a script file on puTTy script pp1.txt
Create a c++ program that: Create an input file using notepad ( .txt ). When testing...
Create a c++ program that: Create an input file using notepad ( .txt ). When testing your program using different input files, you must change the filename inside your program otherwise there will be syntax errors. There are a finite number of lines to be read from the data file. But we can’t assume to know how many before the program executes; so, the standard tactic is to keep reading until you find the “End of File” marker. Input date...
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with...
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with following information: 1,2,3,4,5 red,blue,green,yellow,orange left, right,front, back 2. After having program read the .txt file, output the above information in categories of Symbol, Token Type, and Count : Example: Symbol---Token Type (data type)----Count (how many times symbol appeared in .txt file) =========================================================================== 1 ----digit ----1 2 ----digit ----1 red ----color ----1 blue ----color ----1 left ----direction ----1 right ----direction    ----1
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with...
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with following information: 1,2,3,4,5 red,blue,green,yellow,orange left, right,front, back 2. After having program read the .txt file, output the above information in categories of Symbol, Token Type, and Count : Example: Symbol---Token Type (data type)----Count (how many times symbol appeared in .txt file) =========================================================================== 1 ----digit ----1 2 ----digit ----1 red ----color ----1 blue ----color ----1 left ----direction ----1 right ----direction    ----1
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with...
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with following information: 1,2,3,4,5 red,blue,green,yellow,orange left, right,front, back 2. After having program read the .txt file, output the above information in categories of Symbol, Token Type, and Count : Example: Symbol---Token Type (data type)----Count (how many times symbol appeared in .txt file) =========================================================================== 1 ----digit ----1 2 ----digit ----1 red ----color ----1 blue ----color ----1 left ----direction ----1 right ----direction    ----1
[In Python] Write a program that takes a .txt file as input. This .txt file contains...
[In Python] Write a program that takes a .txt file as input. This .txt file contains 10,000 points (i.e 10,000 lines) with three co-ordinates (x,y,z) each. From this input, use relevant libraries and compute the convex hull. Now, using all the points of the newly constructed convex hull, find the 50 points that are furthest away from each other, hence giving us an evenly distributed set of points.
C Language NO ARRAY UTILIZATION OR SORTING Create a .txt file with 20 integers in the...
C Language NO ARRAY UTILIZATION OR SORTING Create a .txt file with 20 integers in the range of 0 to 100. There may be repeats. The numbers must not be ordered/sorted. The task is to find and print the two smallest numbers. You must accomplish this task without sorting the file and without using arrays for any purpose. It is possible that the smallest numbers are repeated – you should print the number of occurrences of the two smallest numbers....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT