In this article, we will discuss the concept of the C program: Find the frequency of each character in the given string
In this post, we are going to learn how to find the frequency of the each character of the given string in C programming language
The program allows the user to enter a String and then it finds the frequency of the each character in the given string using for loop in C programing language
program 1
#include <stdio.h> #include <string.h> int main() { char str[100]; int i; int freq[256]={0}; printf("Enter a string\n"); gets(str); for(i=0; str[i]!='\0'; i++){ freq[str[i]]++; } for(i=0; i<256; i++){ if(freq[i] !=0){ printf("Character '%c' occurs %d time in te string\n",i,freq[i]); } } getch(); return 0; }
When the above code is executed, it produces the following result
The program allows the user to enter a String and then it finds the frequency of the each character in the given string using while loop in C programing language
program 2
#include <stdio.h> #include <string.h> int main() { char str[100]; int i; int freq[256]={0}; printf("Enter a string\n"); gets(str); i=0; while(str[i]!='\0'){ freq[str[i]]++; i++; } i=0; while(i<256){ if(freq[i] !=0){ printf("Character '%c' occurs %d time in the string\n",i,freq[i]); } i++; } getch(); return 0; }
When the above code is executed, it produces the following result
The program allows the user to enter a String and then it finds the frequency of the each character in the given string using do-while loop in C programing language
program 3
#include <stdio.h> #include <string.h> int main() { char str[100]; int i; int freq[256]={0}; printf("Enter a string\n"); gets(str); i=0; do{ freq[str[i]]++; i++; }while(str[i]!='\0'); i=0; do{ if(freq[i] !=0){ printf("Character '%c' occurs %d time in the string\n",i,freq[i]); } i++; } while(i<256); getch(); return 0; }
When the above code is executed, it produces the following result
Suggested for you
Similar post
C++ program to count the total number of characters in the given string
C program to count the total number of characters in the given string
Python program to count the total number of characters in the given string
Java program to count the frequebcy of a character of a string
C program to count the frequebcy of a character of a string
Python program to count the frequebcy of a character of a string
C++ program to count the frequebcy of a character of a string
Python code to Calculate sum of odd and even in a list In this tutorial,…
How to find reverse number using method In this article, we will discuss the concept…
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…