Letter frequency counter c++: In the previous article, we have discussed C++ Program to Access Elements of an Array Using Pointer. In this article, we will see C++ Program to Find the Frequency of Characters in a String.
C++ Program to Find the Frequency of Characters in a String
- C++ program to print count of all alphabets in a string.
- How to print frequency of characters in a string.
C++ program to print frequency of characters in a string
#include <iostream> #include <cstring> using namespace std; int main(){ char inputString[1000]; // we are using an integer array(initialized with zero) to store // frequency of characters at index equal to their ascii value int i, count[256] = {0}; cout << "Enter a String\n"; gets(inputString); for(i=0; inputString[i] != '\0'; i++){ // Populate frequency count array count[inputString[i]]++; } cout << "\nCharacter Frequency\n"; for(i=0; i < 256; i++){ if(count[i] != 0){ cout <<" " << (char)i << " " << count[i]<< endl; } } return 0; }
Output
Enter a String abbcd Character Frequency a 1 b 2 c 1 d 1
Start writing code in C++ language by referring to Simple C++ Program Examples available here. These examples are helpful to get an idea on the format, function used in every basic program.