16

C program to find the second most frequent charecter in a string

 3 years ago
source link: https://www.codeproject.com/Questions/5318674/C-program-to-find-the-second-most-frequent-charect
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

See more:

we have to find the second most occuring charecter in a string

What I have tried:
Copy Code
#include<stdio.h>
#include<string.h>
void main()
{
	char s[20];
	int count[20]={0},i,first=0,second=0;
	for(i=0;s[i];i++)
	{
		count[s[i]]++;
	}
	for(i=0;i<20;i++)
	{
		if(count[i]>count[first])
		{
			second=first;
			first=i;
		}
		else if(count[i]>count[second]&&count[i]!=count[first])
		{
		 second=i;	
		}
		printf("second most frequent charecter is %c",second);
	}
Comments
What is the problem?

Maybe this will get you started:
Expand ▼   Copy Code
#include<stdio.h>
#include<string.h>

int main()
{
	char s[] = "abca";
	int count[20], i, j, len, first = 0, second = 0;
	
	len = strlen(s);

	for (i=0; i < len; i++)
	{
	    count[i] = 0;
	    
		for (j=0; j < len; j++)
		{
			if (s[i] == s[j])
			{
				count[i]++;
			}
		}
    }
	
	for (i=0; i < len; i++)
	{
		if (count[i] > count[first])
		{
			first = i;
		}
	}
	
	printf("Most frequent character is = %c\n", s[first]);
}

Note that the outcome will not be correct if you have characters with an equal count,
e.g. a string like "abab".

Add your solution here

Preview

Existing Members

Sign in to your account

...or Join us

Download, Vote, Comment, Publish.

Your Email   Password  

 

Your Email   Optional Password  

StrengthToo short

 

I have read and agree to the Terms of Service and Privacy Policy
Please subscribe me to the CodeProject newsletters
When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK