In: Computer Science
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];
}
}
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