on Leave a Comment

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

In this article, you will see C++ Program to Find Frequency of Characters in a String.

TOPICS:

(1) C++ Program to Find Frequency of Characters in a C-Style String.

(2) C++ Program to Find Frequency of Characters in a String Object.

C++ Program to Find Frequency of Characters in a C-Style String

#include<iostream>
using namespace std;
int main()
{
        int i, count=0;
        char str[100], f;
        cout<<"Enter a String : ";
        cin.getline(str,100);
        cout<<"Enter a character to find frequency : ";
        cin>>f;
        for(i=0; str[i]!='\0'; i++)
        {
                if(f==str[i])
                {
                        count++;
                }
        }
        cout<<"Frequency of "<<f<<" is "<<count;
        return 0;
}

OUTPUT:

Enter a String : C++ is a object-oriented programming language.
Enter a character to find frequency : a
Frequency of a is 4

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

#include<iostream>
using namespace std;
int main()
{
        int i, count=0;
        string str;
        char f;
        cout<<"Enter a String : ";
        getline(cin, str);
        cout<<"Enter a character to find frequency : ";
        cin>>f;
        for(i=0; i<str.length(); i++)
        {
                if(str[i]==f)
                {
                        count++;
                }
        }
        cout<<"Frequency of "<<f<<" is "<<count;
        return 0;
}

OUTPUT:

Enter a String : C++ is a object-oriented programming language.
Enter a character to find frequency : a
Frequency of a is 4

0 comments:

Post a Comment