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.
//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; } }
Comments
Leave a comment