
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
C Program to find maximum occurrence of character in a string
An array of characters is called a string.
Declaration
Following is the declaration declaring an array is as follows −
char stringname [size];
For example: char string[50]; string of length 50 characters
Initialization
- Using single character constant −
char string[10] = { ‘H’, ‘e’, ‘l’, ‘l’, ‘o’ ,‘\0’}
- Using string constants −
char string[10] = "Hello":;
Accessing − There is a control string "%s" used for accessing the string till it encounters ‘\0’.
Finding maximum occurrence
The logic to find the maximum occurrence of character is −
- First, find frequency of characters, by using the following program.
while(string[i] != '\0'){ value = (int)string[i]; frequency[value] += 1; i++; }
- Based on this, we are finding maximum occurrence character.
maximum = 0; for(i=0; i<CHARS; i++){ if(frequency[i] > frequency[maximum]) maximum = i; }
Example
Given below is the C program to find max occurring character in a string −
#include <stdio.h> #define SIZE 100 // Maximum string size #define CHARS 255 // Maximum characters allowed int main(){ char string[SIZE]; int frequency[CHARS]; int i = 0, maximum; int value; printf("Enter the string:
"); gets(string); for(i=0; i<CHARS; i++){ frequency[i] = 0; // initialize freq of all characters to zero } /* Finds frequency of each characters */ i=0; while(string[i] != '\0'){ value = (int)string[i]; frequency[value] += 1; i++; } /* Finds maximum frequency */ maximum = 0; for(i=0; i<CHARS; i++){ if(frequency[i] > frequency[maximum]) maximum = i; } printf("Maximum occurrence character is '%c' = %d times.", maximum, frequency[maximum]); return 0; }
Output
When the above program is executed, it produces the following result −
Enter the string: tutorials point Maximum occurrence character is 't' = 3 times.
- Related Articles
- C Program to find minimum occurrence of character in a string
- C# Program to find number of occurrence of a character in a String
- C program to replace all occurrence of a character in a string
- Python program to find occurrence to each character in given string
- Java program to check occurrence of each character in String
- Java program to count the occurrence of each character in a string using Hashmap
- String function to replace nth occurrence of a character in a string JavaScript
- C++ Program to Find the Frequency of a Character in a String
- Finding the last occurrence of a character in a String in Java
- Program to find the kth character after decrypting a string in C++
- Maximum consecutive repeating character in string in C++
- Java Program to Find the Frequency of Character in a String
- Golang program to find the frequency of character in a string
- Python program to find Most Frequent Character in a String
- Python program to find Least Frequent Character in a String

Advertisements