This C++ Program which prints occurence of characters from ‘a’ to ‘z’ in an Input File. The program creates an input file stream, goes through each character in every line of the file stream and increments each character’s count on every encounter of the specific character.
Here is source code of the C++ program which prints occurence of characters from ‘a’ to ‘z’ in an Input File. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C++ Program to Print Occurence of Characters from 'a' to 'z' in an Input File
*/
#include <iostream>
#include <string>
#include <fstream>
#include <cctype>
int main()
{
int arr[26];
std::ifstream file("test.cpp");
std::string str;
for (int i = 0; i < 26; i++)
{
arr[i] = 0;
}
while (getline(file, str))
{
int i = 0;
while (str[i] != '\0')
{
if (isalpha(str[i]))
arr[(str[i] - 'a')]++;
i++;
}
}
std::cout << "Count of character \'a\' - \'z\'\n";
for (int i = 0; i < 26; i++)
{
char c = 'a' + i;
std::cout << c << " " << (arr[i] + '0') << "\t";
if ((i + 1) % 6 == 0)
std::cout << std::endl;
}
std::cout << std::endl;
file.close();
}
$ g++ main.cpp $ ./a.out Count of character 'a' - 'z' a 65 b 48 c 65 d 62 e 68 f 58 g 52 h 53 i 86 j 48 k 48 l 62 m 53 n 65 o 60 p 52 q 48 r 73 s 69 t 83 u 57 v 48 w 50 x 48 y 49 z 49
Sanfoundry Global Education & Learning Series – 1000 C++ Programs.
advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.
Next Steps:
- Get Free Certificate of Merit in C++ Programming
- Participate in C++ Programming Certification Contest
- Become a Top Ranker in C++ Programming
- Take C++ Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Related Posts:
- Buy Programming Books
- Apply for Information Technology Internship
- Practice Computer Science MCQs
- Practice Programming MCQs
- Buy C++ Books