C++ Program to Find Frequency and Position of Characters in a String

This is a C++ Program to Print Each Letter in a String followed by its Frequency.

Problem Description

The program takes a string and prints the frequency of each letter in the string.

Problem Solution

1. The program takes a string.
2. The number of times a letter occurs in the string is recorded.
3. The result is printed.
4. Exit.

C++ Program/Source code

Here is the source code of C++ Program to Print Each Letter in a String followed by its Frequency. The program output is shown below.

  1. #include<iostream>
  2. #include <string.h>
  3. using namespace std;
  4. int main ()
  5. {
  6.     char str[50];
  7.     int i, len, freq[26];
  8.     cout << "Enter a string : ";
  9.     gets(str);
  10.     len = strlen(str);
  11.     for(i = 0; i < 26; i++)
  12.         freq[i] = 0;
  13.     for(i = 0; i < len; i++)
  14.     {
  15.         if(str[i] >= 'a' && str[i] <= 'z')
  16.             freq[str[i] - 97]++;
  17.         else if(str[i] >= 'A' && str[i] <= 'Z')
  18.             freq[str[i] - 65]++;
  19.     }
  20.     cout << "Frequency of characters\n";
  21.     for(i = 0; i < 26; i++)
  22.     {
  23.         if(freq[i] != 0)
  24.         {
  25.             cout << "  " << static_cast<char>(i + 'a') << " : " << freq[i];
  26.             cout << "\n";
  27.         }
  28.     }
  29.     return 0;
  30. }
Program Explanation

1. The program takes a string and stores in the array ‘str’. The length of str is stored in the variable ‘len’.
2. Another array ‘freq’ of size 26 is initialized as 0 to store frequencies of all the letters.
3. Using a for loop, every character in the string is counted and incremented in its corresponding array.
4. The loop terminates when the string ends.
5. The list of letters and their frequencies are printed if frequency is not 0.

advertisement
advertisement
Runtime Test Cases
Case 1 :
Enter a string : Ball
Frequency of characters
  a : 1
  b : 1
  l : 2
 
Case 2 :
Enter a string : ababababab
Frequency of characters
  a : 5
  b : 5
 
Case 3 :
Enter a string : Hello World!
Frequency of characters
  d : 1
  e : 1
  h : 1
  l : 3
  o : 2
  r : 1
  w : 1

Sanfoundry Global Education & Learning Series – C++ Programs.

To practice all C++ programs, here is complete set of 1000+ C++ Programming examples.

Note: Join free Sanfoundry classes at Telegram or Youtube

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.