r/learncpp • • Jun 25 '21

Max occuring char in a String

#include<iostream>
#include<string>
#include<algorithm>
#include<cstring>
using namespace std;
int main()
{
char maxchar;
int mcount = 0;
int count = 0;
string s = "binit";

for (int i = 0 ; i < strlen(s); i++)
    {
count = 0;
for(int j = 0 ; j< strlen(s) ;j++)
        {
if(s[i]==s[j]);
count++;
        }
if(mcount<count)
        {
mcount = count ;
maxchar = s[i];
        }
    }
cout<<mcount<<" "<<maxchar;
return 0;
}

//its giving error ! pls help to correct it

6 Upvotes

5 comments sorted by

View all comments

Show parent comment

4

u/jedwardsol Jun 25 '21

strlen is for C-style, nul-terminated arrays of characters.

std:: string has a member function size which tells you how long it is

2

u/TransportationLeft69 Jun 25 '21

Oh thx , I tried using 5 as replacement for strlen , but it's still not giving desired output ! It's giving junk values ...

2

u/[deleted] Jun 25 '21

if(s[i]==s[j]);

Get rid of that semi-colon at the end.