64 679
Assignments Done
99,2%
Successfully Done
In September 2018

Answer to Question #55384 in C++ for Gagan

Question #55384
Write an interactive c++ program to open a text file and the display the following:
1) Frequency table of all the alphabetic characters present in the file.
2)Number of numeric characters present in the file.
Expert's answer
#include <iostream> // std::cin, std::cout
#include <fstream> // std::ifstream

int main()
{

//limits of array
const int start = '0';
const int end = 'z';
const int size = end - start + 1;
//array that will store information about the characters and their number
int data[size];

//fill array initial value
for (size_t i = 0; i < size; i++)
{
data[i] = 0;
}

char c; //buffer character

char * file_name = "file.txt"; //choose your own file and write here
std::ifstream is(file_name); // open file
while (is.get(c)) // loop getting single characters
{
int index = c;
if (index >= start && index <= end)
data[index - start]++;
else
continue;
};
is.close(); // close file

//show information about file
for (size_t i = 0; i < size; i++)
{
if (data[i] != 0)
{
char c = i + start;
std::cout << c << " character occurred " << data[i] << " times" << std::endl;
}
}

return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions