Question

In: Computer Science

I have a C++ question. I'm reading words from a text file and storing each words...

I have a C++ question. I'm reading words from a text file and storing each words in an array. Currently I'm doing it like the code below and its working fine but I need all the words to be stored in the array as lower case letters. How can I do this? Thank you.

if(file.is_open())

    {

        for(int i = 0; i < arrayLength; i++)

        {

            file >> array[i];

        }

    }

Solutions

Expert Solution

Here iam providing the code for the given program with explanation with comments.

Code in text Foramt:-you can copy it and you can test the code

#include <iostream>   
using namespace std;
#include <fstream>   

int main () {
char filename[300];   //declaring filename for getting from user

cout << "Enter the name of the file: ";   //ask for filename
cin>>filename;    //storing the name of the file in filename variable
  
std::ifstream file(filename); //opens the file
char array[500];       //declaring character array with some assumed size
char chr;   //delcare a variable chr for storing each character.
int len=0;   //delare len variable for stroing the length number of characters
while (file.get(chr)) //get a character from file and store it in chr
{   if(chr>='A' && chr <='Z')   //check if it is Capital letter or not if it is Capital add 32 because the difference between them is 32
   {
       chr=chr+32;
   }
array[len++]=chr;   //store chr in array and increment the len each time
   }  

//traverse the array and print the words in the file as small letters
for(int j=0;j<len;j++)
{
       cout<<array[j];
   }
file.close(); // close file

return 0;
}

Sample Output:-

Text in the file:-

File.txt

hEllo my name IS venkaTesh
Hello NicE to Meet You

If you have any queries please Comment

If you understand the answer please give me a thumbs up.Thanking You


Related Solutions

In C++, write a program that accepts a text file of ASCII words from standard input...
In C++, write a program that accepts a text file of ASCII words from standard input and store them and the amount of times the word appears in the file in a hash table using external chaining. Then print the words and their counts sorted based on alphabetical order and print them again in decreasing numerical order based on the amount of times the word appears in the file. Space, tab, and new line all count as space characters. The...
assignment in C I have a file that contains a lot of lines with words separated...
assignment in C I have a file that contains a lot of lines with words separated by spaces ( also contains empty lines as well). I need to read this file line by line and put each word into 2d array. NOTE: i need to skip spaces as well as empty lines. also I need to count each word.
For c language. I want to read a text file called input.txt for example, the file...
For c language. I want to read a text file called input.txt for example, the file has the form. 4 hello goodbye hihi goodnight where the first number indicates the n number of words while other words are separated by newlines. I want to store these words into a 2D array so I can further work on these. and there are fewer words in the word file than specified by the number in the first line of the file, then...
● Write a program that reads words from a text file and displays all the words...
● Write a program that reads words from a text file and displays all the words (duplicates allowed) in ascending alphabetical order. The words must start with a letter. Must use ArrayList. MY CODE IS INCORRECT PLEASE HELP THE TEXT FILE CONTAINS THESE WORDS IN THIS FORMAT: drunk topography microwave accession impressionist cascade payout schooner relationship reprint drunk impressionist schooner THE WORDS MUST BE PRINTED ON THE ECLIPSE CONSOLE BUT PRINTED OUT ON A TEXT FILE IN ALPHABETICAL ASCENDING ORDER...
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.
C++ coding question From the text file given to you- “worldpop.txt”, perform the following tasks using...
C++ coding question From the text file given to you- “worldpop.txt”, perform the following tasks using Boolean function. PS-*Write separate codes for each task* Task 1. Display the names of the countries with: 1. Population >=1000,000,000 2. Population <= 1000,000 Task 2. Display the names of the first 10 countries Task 3. Display the names of the last 10 countries contents of worldpop.txt: Afghanistan 32738376 Akrotiri 15700 Albania 3619778 Algeria 33769669 Andorra 72413 Angola 12531357 Anguilla 14108 Argentina 40677348 Armenia...
Write a C++ program that reads a string from a text file and determines if the...
Write a C++ program that reads a string from a text file and determines if the string is a palindrome or not using stacks and queue
Hi! I have a homework question I'm not sure if I'm doing it correctly. Below is...
Hi! I have a homework question I'm not sure if I'm doing it correctly. Below is the information from the question and my answer in bold. Glass Company makes glass orders based on the customer specifications, so the company uses job costing to track costs. The company uses direct labor hours as the cost driver for manufacturing overhead application. Manufacturing overhead costs for the year:        $787,500 Usage of direct labor hours for the year:               225,000 Beginning Work-in-process, March 1 (Job 57)      $80,000 Beginning...
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...
General overview (In C++) In this program you will be reading sales information from a file...
General overview (In C++) In this program you will be reading sales information from a file and writing out a bar chart for each of the stores. The bar charts will be created by writing out a sequence of * characters. You will have to create input files for your program. You can use a text editor such as Notepad or Notepad++ to create this. There may also be an editor in your IDE that you can use. You can...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT