Write a C++ Program to Find the Frequency of Characters in a String

C++ Program to Find the Frequency of Characters in a String

Welcome to the World of Online Learning:

Hello Friends “This blog helps you to learn C++ programming concepts. You can learn C++ language at your own speed and time. One can learn concepts of C++ language by practicing various programs given on various pages of this blog. Enjoy the power of Self-learning using the Internet.”

C++ Program to Find the Frequency of Characters in a String
C++ Program to Find the Frequency of Characters in a String

Write a C++ Program to Find the Frequency of Characters in a String

PROGRAM:C++ Program to Find the Frequency of Characters in a String

/* C++ Program to Find the Frequency of Characters in a String*/

1.Find Frequency of Characters of a String Object

#include <iostream>
using namespace std;

int main()
{
string str = “C++ Programming is awesome”;
char checkCharacter = ‘a’;
int count = 0;

for (int i = 0; i < str.size(); i++)
{
if (str[i] == checkCharacter)
{
++ count;
}
}

cout << “Number of ” << checkCharacter << ” = ” << count;

return 0;
}

2: Find Frequency of Characters in a C-style String

#include <iostream>

using namespace std;
int main()
{
char c[] = “C++ programming is not easy.”, check = ‘m’;
int count = 0;

for(int i = 0; c[i] != ‘\0’; ++i)
{
if(check == c[i])
++count;
}
cout << “Frequency of ” << check << ” = ” << count;
return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *