This is a C++ program to count number of occurrences of any character in a string.
In this program we have accepted a string and character whose occurrences we have to find.
Let see C++ program for this problem.
#include<iostream>
using namespace std;
int main()
{
string str;
char ch;
int count=0,i,flag=0;
cout<<"Enter a String:";
getline(cin,str);
cout<<"Enter any character:";
ch=getchar();
for(i=0;i<str.length();i++)
{
if(str[i]==ch)
{
flag=1;
count++;
}
}
if(flag==1)
{
cout<<"Character "<<ch<<" occurred in String "<<count<<" times."<<endl;
}
else
{
cout<<"Character "<<ch<<" is not present in String"<<endl;
}
return 0;
}
INPUT && OUTPUT
0 Comments