

C program to find the second most frequent charecter in a string
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.

See more:
What I have tried:
#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); }
#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
...or Join us
Your Email Password
Your Email Optional Password
Please subscribe me to the CodeProject newsletters
- Read the question carefully.
- Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
- 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.
- 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.
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)
Recommend
-
64
Samebug has a large collection of crashes from the web: stack traces with a web page, where we found it, attached. We grouped these crashes based on a number of criteria: exception type, software…
-
62
README.md Imbalanced Dataset Sampler
-
32
Google Camera 7.1 reveals work on a “Frequent Faces” feature, possibly for the Pixel 4Next week, Google will officially unveil the Pixel 4 and other Pixel-branded products at the annual
-
11
-
9
题目¶ 原题地址:https://leetcode.com/problems/most-frequent-subtr...
-
8
Java program to find second largest number in an array This is familiar interview question for the middle level developers. Interviewer will check the coding logic of the candidate and best solution(performance level).
-
8
Python program to find most frequent element in a listSkip to content Python program to fin...
-
9
Java program to find the first and last character of a stringSkip to content
-
6
Check if most frequent element in Linked list is divisible by smallest elementGiven a Linked list, the task is to check if the most frequently occurring element in the linked list is divisible by the...
-
5
Java program to find the Second largest number in an Array or List Given an unsorted array of an integers or a List, to write a Java program to find the second largest number or element from the Array or a List1) Given in...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK