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.
1
Expert's answer
2015-10-09T02:30:57-0400
#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 the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS